0% found this document useful (0 votes)
3 views

C# unity access modifier

The document explains the concept of scope and access modifiers in C# and Unity, detailing four access modifiers: public, private, protected, and internal. It provides examples of a PlayerController class and a ScopeExample class to illustrate how these modifiers affect the accessibility of variables and methods. The document also highlights the output of accessing various fields and methods within the classes, demonstrating the practical application of these access modifiers.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

C# unity access modifier

The document explains the concept of scope and access modifiers in C# and Unity, detailing four access modifiers: public, private, protected, and internal. It provides examples of a PlayerController class and a ScopeExample class to illustrate how these modifiers affect the accessibility of variables and methods. The document also highlights the output of accessing various fields and methods within the classes, demonstrating the practical application of these access modifiers.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

In C# and Unity, scope refers to the visibility and accessibility of a variable, method, or class within a

program. Access modifiers, on the other hand, specify the level of access that is allowed to a
member within the scope in which it is defined. There are four access modifiers in C#:

public: The member can be accessed from anywhere in the program.


private: The member can only be accessed within the same class where it is declared.
protected: The member can be accessed within the same class and any derived classes.
internal: The member can be accessed within the same assembly (i.e., the same .dll
or .exe file).

Here is an example program that demonstrates scope and access modifiers in C# Unity:
using UnityEngine;

public class PlayerController : MonoBehaviour


{
public float speed = 5f; // public member can be accessed from other scripts and Unity editor

private Rigidbody2D rb; // private member can only be accessed within this class

void Start()
{
rb = GetComponent<Rigidbody2D>(); // GetComponent() is a public method of the
MonoBehaviour class
}

void FixedUpdate()
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");

Vector2 movement = new Vector2(moveHorizontal, moveVertical);

rb.AddForce(movement * speed); // using a private member in this class


}
}

In this example, the PlayerController class has a public member speed that can be accessed from
other scripts and the Unity editor. The class also has a private member rb that can only be accessed
within this class. The Start() method, which is a public method inherited from the MonoBehaviour
class, uses the public GetComponent() method to assign a value to the private rb member. Finally,
the FixedUpdate() method, which is also a public method inherited from the MonoBehaviour class,
uses the private rb member to apply force to the game object's rigid body.

Four access modifiers with C# unity with simple program


In C# and Unity, access modifiers are keywords used to specify the accessibility of classes, methods,
fields, and properties. There are four access modifiers in C# and Unity:

Public: Public access modifier allows the class or member to be accessed from anywhere in the
code. This is the most permissive access modifier.
public class Player {
public int score;
public void Jump() {
// Code for player jumping
}
}

Private: Private access modifier allows the class or member to be accessed only within the
class where it is defined. This is the most restrictive access modifier.
Example:
public class Player {
private int health;
private void TakeDamage() {
// Code for player taking damage
}
}

Protected: Protected access modifier allows the class or member to be accessed within
the class and its subclasses.
public class Enemy {
protected int health;
protected void TakeDamage() {
// Code for enemy taking damage
}
}

public class Boss : Enemy {


public void SpecialAttack() {
// Code for boss's special attack
}
}

Internal: Internal access modifier allows the class or member to be accessed within the
same assembly (a group of related code files that are compiled together) but not outside
the assembly.
internal class Helper {
internal void DoSomething() {
// Code for doing something
}
}

public class Player {


private Helper helper;
public Player() {
helper = new Helper();
}
public void UseHelper() {
helper.DoSomething();
}
}
Access.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ScopeExample : MonoBehaviour


{
// Public field accessible from other classes
public int publicNumber = 10;

// Private field accessible only within this class


private int privateNumber = 20;

// Protected field accessible within this class and its subclasses


protected int protectedNumber = 30;

// Internal field accessible within this assembly (project)


internal int internalNumber = 40;

// Start is called before the first frame update


void Start()
{
// Accessing all the fields within this class
Debug.Log("Public Number: " + publicNumber); // Output: Public Number: 10
Debug.Log("Private Number: " + privateNumber); // Output: Private Number: 20
Debug.Log("Protected Number: " + protectedNumber); // Output: Protected Number: 30
Debug.Log("Internal Number: " + internalNumber); // Output: Internal Number: 40

// Creating an instance of a helper class that is internal to this assembly


Helper helper = new Helper();

// Accessing the helper's public method


helper.PublicMethod(); // Output: Public helper method

// Trying to access the helper's private method (which is not accessible from outside the helper
class)
//helper.PrivateMethod(); // This line will produce a compile error
}

// Update is called once per frame


void Update()
{

// Private method accessible only within this class


private void PrivateMethod()
{
Debug.Log("Private method");
}
// Public method accessible from other classes
public void PublicMethod()
{
Debug.Log("Public method");
}

In this example, we have a class ScopeExample that has four fields with different access
modifiers (publicNumber, privateNumber, protectedNumber, and internalNumber). We also
have four methods with different access modifiers (PublicMethod, PrivateMethod,
ProtectedMethod, and InternalMethod). Finally, we have a nested class Helper that has a
public method PublicMethod and a private method PrivateMethod.
In the Start method, we access all the fields and create an instance of the Helper class. We
can call the PublicMethod of the Helper class, but not the PrivateMethod, since it is not
accessible from outside the Helper class.

When we run this code, we get the following output in the console:

Output:
Public Number: 10
Private Number: 20
Protected Number: 30
Internal Number: 40
Public helper method

// Protected method accessible within this class and its subclasses


protected void ProtectedMethod()
{
Debug.Log("Protected method");
}

// Internal method accessible within this assembly (project)


internal void InternalMethod()
{
Debug.Log("Internal method");
}

// Nested helper class with private and public methods


private class Helper
{
public void PublicMethod()
{
Debug.Log("Public helper method");
}

private void PrivateMethod()


{
Debug.Log("Private helper method");
}
}
}

You might also like