Unity Access Modifiers

Unity Access Modifiers


Unity Access Modifiers


What is an Access Modifier? 

An access modifier is a type of compiler code that we use to specify and adjust whether the variables and classes in the program we write can be accessed by other classes.
We use access modifiers to ensure the security of the code of our program/game.


Access Modifiers Between Codes

Private

It is the access modifier that we use to define hidden values and ensures that the value it is assigned to is only accessible from its own class.

You can use it for values/variables in your code that absolutely must not be changed.

private int x


Public

This modifier allows values to be accessed and modified from other classes and contains no restrictions.

public int x


Protected

It is the access modifier that makes the assigned value protected, and the value can only be accessed by classes derived from the same class.

protected int x


Internal

These are values that can be accessed from within the same program, but not from another program.

internal int x


Protected Internal

A value defined as protected internal can be accessed from within the class it is defined in and from classes derived from it. It does not matter if the derived class is not in the same program.

protected internal int x 


Editor Access Modifiers

SerializeField

Regardless of whether they are public or not, it makes all the variables we define visible in the Inspector window in the editor.

[SerializeField]


HideInInspector

It hides variables that are set to public or made visible in the editor with SerializeField from the editor.

[HideInInspector]