Variables! - The building blocks of programming.
Objective: Explain what variables are and how we use them.
What are variables?
Variables are where data or information are stored. A username, a players health or score, how fast they can run, whether they’ve been hit, died or won the game. All of this information has to be stored so it can be accessed or changed and that is where variables come in.
What makes up a variable?
Variables need three things with a partially optionally fourth.
- An accessor or reference.
For game development this is generally set to Public or Private. A Public variable allows the outside world, other scripts and objects to see and access it. This can be useful when calling or setting some variables like health from an enemy script when the enemy hits the player to reducing the players health.
For most variables though Private is a safer option as only the object with the variable(within a script) attached to it knows it exists. This prevents other scripts or objects from accessing or modifying the variable which can help to reduce bug, errors or misbehaving objects or elements(like modders creating cheats). To allow other programmers, designers or any other contributors to the game projects Private variables can be set as modifiable in the Unity editor by serializing the variable within the script. This keeps its privacy from other objects or scripts but allows users to modify it in the editor.
2. A Data Type.
There are four common data types.
String: Written as string. These are text values and can include letters, numbers or characters and are generally wrapped within “double quotes”.
Interger: Written as int. These are whole numbers and can be positive or negative (-1 , 0, 1).
Floating-point: Written as float. These are decimal values and carry an f at the end of the value. They can also be positive or negative(-3.5f, 2.65f).
Boolean: Written as bool. These are simple a true or false value(Yes or No, On or Off).
3. A Name.
All variables must be assigned a name. These must be unique to a certain degree. There are Global and Local variables. You can’t have two global variables of the same name.
Global variables are set for the entire class or script and outside of methods/functions such as Start, Update, Hit, Powerup. Global variables are accessable within these functions/methods.
In the above figure we have the variables username and health. Both are global and have no values set initially. Being global variables, they are accessible within the Start function where they can have their values assigned. Directly within the Player class(public class Player) we set Global variable within the curly braces { }. The void Start creates a seperate block within its own curly braces. Inside this block you can create local variables or modified the global variables. Variables created within the block are not accessible outside the block.
The variable speed is a local variable and can’t be accessed outside the Start function. If we create a local variable with the same name as a global variable the global variable will not be accessible within that function even if the variable is of a diffent type.
In the above figure we have set a global variable int health with no value assigned. The Start function is trying to assigned 100 to the health but because Start has a local variable with the same name, even though it has a different data type the global variable health will not be assigned and this can cause an error stopping the program from running.
The best practive is to keep all variable names unique to avoid issues and if possible to try and stick to global variables. These global variables can all be created at the top of the class so even if the script has hundreds of lines of code, functions etc, they will be easier to maintain.
4. A Value.
The partially optional part needed when creating a variable. As shown in previous examples, when creating a variable you have the choice to assign the value directly or to assign it later within functions/methods.