C# Variables in Unity
This article covers what variables are, why and how we use them in Unity. Variables are used in used in every programming language only the grammar (aka syntax) changes, so this knowledge is widely applicable. In Unity when we create a script we use the programming language C#.
What are variables?
You can see variables as boxes. You put some information in it and tell the computer to use the information from that box to do something with a script.
Why use variables?
For example you get stats-information on an epic sword you can find in your game. It does 9999 damage, 3 meters long, need both hands to use it etc. Now if you are writing a script to attack and deal damage, you want to know how much damage to deal. It is possible to enter the number 9999 manually. If you write an inventory view script you could manually enter 9999 for the epic sword again. Later you might discover the epic sword is overpowered and you want to nerf it. You will have to go to each script and change those numbers manually. This is where the box (aka variable) is useful. Instead of entering the information manually and changing them in each place you used it you tell the scripts to use the epic sword damage box. Now if you need to change the information later you only need to change the information in the box. The computer will just look in the box for information when you tell it to do something with your scripts.
4 basic variable types
There a various types of variables just like boxes. To store a teaspoon you don’t need a huge box. Just like a room in which you can store boxes in, a computer also has a finite space to store variables. Nowadays computers are very powerful and have a lot of space compare to the early days. Still this not infinite and if you get too many boxes or aren’t cleaning up regularly you run out of space.
When you create a variable you pick a box. You could choose to use only gigantic boxes but the room fill be filled quickly. A variable type is the type of box. We cover the 4 basic types.
The integer type variable stores whole numbers only. -3, -2, -1, 0, 1, 2, 3 etc
The float type variable stores numbers with a decimal point.
This means ‘choosing a bigger box’ to be able to store numbers that don’t fit in the integer box like 381.15346879.
The string type variable is used to store text.
Text for the computer doesn’t exist. The computer stores a bunch of numbers and displays them as letters. This means a number for each letter, space, dot etc. This will have to be quite a large bug.
The bool type variable stores ‘true’ or ‘false’.
This is stored as a 1 and 0 respectively, which results in only needing a tiny box.
By using the correct variables (box types) you can run your game on more machines. Optimized games run smoother and drain less battery which is relevant with so many mobile games.
Public or private
When creating a variable you can choose to make it public or private. Public means other scripts can find and private means the opposite. If you don’t choose private or public it will be automatically private. Recommended is to always type it out for better readability. It’s better to keep all variables private unless it’s needed for other scripts to access the variable.
It’s possible to keep the variable private but viewable in the Inspector in Unity by adding: [Serializefield]
Unity Inspector overwrites
As seen in the images above always remember that the values put in the inspector overwrite the values from the script. The speed is set to 5 in the script, but in the inspector it’s set to 8. The game will use the value 8.