Some users, especially those new to Unity3d, may have issues understanding variables and declarations within a script. I wanted to take a moment to explain in-depth the different types of variables, how they work, and what your own best practice should be for the usage of each.
var myVariable : String;
public string myVariable;
A public variable is two things:
- A variable which can be accessed by other scripts if needed
- Available in the Unity editor for quick access and alteration
Regarding the latter, a quick note should be made. If a public variable is declared and compiled, it will remain the same in the editor even if a dev later changes it in the original script where it is first declared. For example, if you write something like:
var myVariable : int = 25;
…and then save/compile it (see it in the unity editor), if you change the script to something like:
var myVariable : int = 50;
…then nothing would actually change. This one thing is confusing to every new unity user (even experienced developers used to other dev tools).
On the other hand, one of the main advantages about public variables is the ability to utilize them anywhere. Let’s set up an example here.
//This script is ScriptOne.js, and simply makes a public variable. var scriptOneVar : int = 25;
//This is ScriptTwo.js, and it accesses the variable information from the other script. var script : ScriptOne; script = GameObject.GetComponent("ScriptOne"); var accessedVariable : int = script.scriptOneVar;
This same thing will work with public functions as well, but that should be another tutorial altogether. For now, just know that due to this nifty functionality, you need not declare a variable multiple times for multiple scripts. Using GetComponent, you can access a script (along with its variables) from any gameObject the script is attached to.
A static variable is one that will be active from the very beginning of the program’s execution. For this reason, you may want to use them more sparingly, because they will need to be changed manually through script each time, and cannot simply be destroyed along with gameObjects.
//Script name myScript.js static var myVariable : String = "Steve";
If the code above is used, for example, the variable is equal to “Steve” from the moment the game begins to run. Static variables can only be declared or altered through script, but I myself have found them handy for overall usage in things that are altered as a player moves from one level to the next. If you have a script that only contains static variables, you should never need to attach that script to any particular gameObject.
Accessing these variables is as easy as pointing to the script name. In a project where the above script exists, a separate script that says:
//Other script print ("The character's name is" + myScript.myVariable);
…should output the static value. In the same vein, changing the value is as easy as:
myScript.myVariable = "Justin";
A private variable is hidden from the inspector and only usable/accessible from the script it is executed from. Using these will keep your inspector looking cleaner and make things easier on bigger projects overall. Declaring them is quite simple:
private var myVariable : int = 55;
By default, I will use a private variable each and every time. Only if I know that I will need to alter something via the inspector or access the value from another script will I start to decide on other types. When programming, I find things easier overall if you plan for any project to get much larger than intended, and using private variables will make your code more manageable for the future.