Variables — Building Blocks of Programming
A common analogue used to describe variables is that of boxes to store different types of things. You’ve got jewelry boxes, shoe boxes. Similarly in programming there are boxes for different types of data. These are variables, because what they contain can vary.

All of programming is basically creating, using, and changing variables as a particular piece of software is running.
For instance in the below screen, many elements of the UI are a variable running in the background, whether it’s the player health, amount of ammo they have, perhaps number of flags their team has captured.

The following snippet can be viewed as a basic building block. Below are what each part is.
public int numberOfLives = 3;
- public — access modifiers. In Unity there’s public and private. Public variables can be seen and edited in the Script component in the Inspector tab. Other game objects can also access public variables (or methods). Private variables can only be accessed within the script itself but if annotated with a [SerializeField] it can also appear in the Unity editor for game designers to tweak.
- int — this part specifies the data type, in this case integers, or whole numbers. Other common data types in Unity are float (numbers with decimals), string (words), and bool (True/False values). There are many others too, and even the script itself is a data type.
- numberOfLives — this is the name of the variable, and can be anything, preferably something that is easily understood in the scope of its use, and for other developers looking at your code. It is written in a style called camel case, where the first word is lower case but subsequent words are capitalized. For private variables the convention is to add an underscore prefix, for example _playerLives.
- = 3 — this means we assign a value of 3 to numberOfLives. Variables will have a default value if you do not assign a value yourself when you first declare one, such as zero, or null. This value can be updated as the program continues.
One last thing to note is where a variable is declared. If it’s declared inside a block like the _spawnManager in the code below, it means it is a local variable and this variable can only be used inside the Start() method block. Other methods cannot use _spawnManager unless Start() gives it to them in a method argument.

However if you declared this variable near the top of the script, then it is called a global variable that all methods inside the script can use.
