FALSE a static var is *NOT* a global var. `public` makes a variable global, not static.
//This is perfectly legal
private static int myPrivateVar;
static makes a variable a class variable (its a little different than it is in C++) which means that it is associated with a class, not any of its instances. Public makes a variable or function accessible from outside functions. Hence the use of properties to encapsulate fields.
Unity will let you inspect any serializable public instance variable. Now that can sound intimidating if you aren't a programmer so let me break that down:
- Serializable. All the Unity types, Mono objects, and the basic data types (int, float, string), and any classes you create that have the [Serializable] attribute.
- public. The easiest one, they can't be private or protected.
- Instance variable. As long as it isn't static, then its an instance variable.
The proper way to access a global variable (besides being through a property) is to get an instance of the class then read its value.
//C#
var instance = (MyClass)FindObjectOfType(typeof(MyClass));
Debug.Log(instance.variableName);
↧