Unity C# — The Basic of Variables
At the base of nearly all programming languages, there are variables!
C#, the core of Unity Engine, is no exception.
In this story, I will go over some common variable types of C# used in Unity Engine. Also, some specific class references of the Unity API named MonoBehaviour.
First and foremost,
What is a Variable
Variables, in programming, are containers for storing data values.
The big quirk is that they are meant to be changing as the program runs.
In C#, there are different types of variables named Data Type. C# has more than a dozen Data Types built-in. You can use all of them, but in this story, I will only go over the most important to know in Unity:
C# Data Types
int — stores integers (whole numbers), without decimals, such as 12 or -846
float — stores floating-point numbers (4 bytes), with decimals, such as 1f, 987f, 12.5984f, 0.12f. //Used everywhere in Unity Editor and most used. Also, you always need to add an “f” after your value to signify “float” and not a double.
double — stores floating-point numbers (8 bytes), with decimals, such as 2, 489, 2.584801, 0.28. //Less used in Unity.
string — stores text, such as "Hello World".
bool — stores values with two states: true or false.
Unity API Reference Types
Unity uses some custom classes to make some combinations of the above types to make it easier to use in game development environments. Don’t worry about what a class is as for now.
vector2 — Representation of 2D vectors. //2D array of 2 floats (x,y)
vector2int — Same as vector2 but with ints.//2D array of 2 ints (x,y)
vector3 — Representation of 3D vectors.//3D array of 3floats (x,y,z)
vector3int— Same as vector3 but with ints. //3D array of 3 ints (x,y,z)
vector4 — Representation of 4D vectors. //4D array of 4 floats (x,y,z,w)
GameObject — Representation of 1 Unity GameObject with all its parameters.
Where to use Variables
One use of variables is that since it is a changing value, you can use it instead of a hardcoded value.
Imagine that you have to use the name of one object in multiple places. You did something like that: “Hey! Here is your Super Object!”. Like in NPC text, signs, UI or any other place you may need to have this name showed or read. You would need to put the name manually at every place.
Once all done… You may want to change that object name. You would need to go and change all the occurrences manually again.
Instead of doing so, you can use a variable to store the string name of the object. Then use it variable instead. Now it may look like it: “Hey! Here is your ” + itemName + “!”.
You do still need to write the variable multiple time, but only once! If you change the variable value, it will change at every place that you are reading it.
Declaring (Creating) Variables
To create a variable, you must specify the type and assign it a value:
type variableName = value;
Where type is a C# type (such as int
or string
), and variableName is the name of the variable (such as x or name). The equal sign is used to assign values to the variable. If there is no equal the variable will either be null or 0.
You can then use the variable where ever you want (where it is accessible).
The easiest to test is:
Debug.Log(variableName);
You can also use “var”, but its use is not in the scope of this story.
Change Variables Value
Once a variable declares, you can then re-assign the value when even you want by doing so, without the data type:
variableName = newValue
Use Variables Value
As you now know, you can set value and read it.
last information for you is that you can apply mathematic expressions to variables like so:
int myInt1 = 5; //5
int myInt2; //0myInt2 = 6 //Now equal 6
int result = myInt1 + myInt2; // result is equal to 5 + 6
Debug.Log(result); //Print: 11
Constant Variables
For this last part, let see what is a Constant.
Simply put, it is a variable that… don’t vary!
You may ask: Why make a variable that is constantly the same value? Isn’t it contradictory to what a variable is?
The particularity of a constant is that it can be changed, but only when declared. It cannot be changed after that.
You can use it to store a value that will never change during execution.
To declare a constant you simply need to add “const” before the value type. Note: You always need to give it a value right away.
Example: In your game, an enemy cannot have more than 3 lives. You can set that maximum to never change.
const int maxLives = 3;
Bonus lecture: Naming Conventions