Access modifiers

In Unity, when declaring class-scope variables, we need to declare an access modifier. The options available are private, public, or protected. If no access modifier is chosen, private is assumed. One of the main reasons to make variables in Unity public is to be able to access them in the Unity Inspector.

In C#, this means:

  • Private: This means the access modifier cannot be accessed by other scripts
  • Public: This means the access modifier can be accessed by other scripts
  • Protected: This means the access modifier can only be accessed by internal or derived sources

In Unity, this additionally means:

  • Public: A variable shows up in the editor and can be changed outside of the script
  • Private: Variables do not show up in the editor
  • Protected: A variable can only be accessed by internal or derived sources
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.65.134