Variables the foundation of every game that you play, from the speed of blocks dropping in the puzzle game Tetris or the health of your character in a role-playing game. Variables are essentially containers for data that allow you to store data and use it seamlessly. Variables make it easy to identify what you’re looking for so you’re focusing more on what the logic is trying to do instead of wondering what that 5 or 500 value is for.

Just For Reference: Value Types and Reference Types

Variables can broad broken up into two groups. Value types and Reference types.

A Value type essential holds a value in it’s container like an actual number or state.

Reference types are a little trickier to understand but simply it doesn’t hold a value directly but instead holds the address where the value is being stored. Examples of both types are shown below this is no means an exhaustive list.

The basic syntax structure for both value types and reference types are the same and is as follow. You can define the value or reference but it’s not strictly necessary. camelCase is encouraged to help differentiate it from methods that CapitalizeTheFirstOfEveryWord and every variable is terminated with a semicolon to let the compiler know that variable is done.

[Access Modifier] [Value/Ref Type] [Name in camelCase] [semicolon]

Value Type: “public int moneySpent;”

Reference Type: “private GameObject enemy:”

A good way to visualize both types is a Player’s stats like health, armor, money, and it’s Transform. Such as shown in the commented code below.

Access Granted: Variable Access Modifiers

Another important feature of variables are access modifiers. These act as gatekeepers of information for your scripts in Unity. They largely break down into 2 types for most intents and purposes, there are more types but these are the two you must absolutely know.

Public: Freely available to any script anywhere in the project without restriction, this should only be used for information that absolutely must be changed. Good coding standards dictate that you use public access modifiers sparingly if at all.

Private: Only available to the class or script that created them. This should be your default choice for a variable unless you have a good reason for why something else is changing it from outside that particular script or class.

Protected: The type or member can be accessed only by code in the same class, or in a class that is derived from that class. This is useful for inheritance when the derived class needs to change the values of the parent class. For example and enemy unit changing the health to be smaller or larger than the base class’ health. A tank might have more health than an infantry man but they’re both derived from the Unit base class. However these cannot be accessed outside the derived classes.

Other Variable Accessors: Internal, Protected Internal, Private Protected. These are fairly uncommon in most Unity programming but have their uses. You can learn more about them here at MSDN.

Going back to this scripting example you can see that there are private and public variables, things like health, and speed on Player aren’t accessible outside of this class (nor should they be) but whether the player is alive or not is so that enemies know when to stop attacking or when to keep going.

As you develop more and more scripts you will begin to see need for access modifiers to prevent variables from being changed accidentally as more and more scripts interact with one another which can cause unexpected bugs. Until next time, happy coding.