C# unity access modifier
C# unity access modifier
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#:
Here is an example program that demonstrates scope and access modifiers in C# Unity:
using UnityEngine;
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");
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.
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
}
}
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
}
}
// 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
}
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