Unity Fundamentals — Variables the core of every program

In the last series of articles, we created a Prototype of a simple shooter game where we used some of the most basic functionality to show what is possible and make you feel comfortable with the possibility you have with Unity.

Before we move on to Creating our first real game I want to take this opportunity to go a little deeper into some of the building blocks we have been using so far. Today we dive a little deeper into Variables.

Variables

What are they and how do we use them? You might have heard the comparison that a Variable is a box you can put data in and this is quite accurate but I want to explain it a little deeper.

A variable is a container in which you can store any type of information so you can reference it later.

You can see it as a vending machine that has all sorts of candy in it. Now your friend always gets you your favorite candy if this candy is always the same it is easy to tell them the brand and type you want. But what if you change your taste daily or the candy runs out? You could have your friend ask you every day which one you want but this is highly inefficient, so we introduce a “variable” we tell our friend our “favoriteCandy” is always in slot “C1” this means that when we want another candy we don't have to change the instruction given to our friend but we just put another type of candy in the C1 slot and whatever happens we will always get whatever is in there.

Photo by Ji Seongkwang on Unsplash

In code this works especially well since the instructions are way more complicated than just asking your friend to get your favorite bar but there might be 100 bars and 10 different friends so you have to edit every single person's instruction every time you change something.

Variable “Data” Types

Now that we know what we are storing we need to declare what “Type” of variable we are using. There are a lot of types of variables with different data types, Like there are also different vending machines for different products

Like you can not put Milk in a cardboard box you can also not put a “bool” (True or False) variable in a “int” container (integer numbers).

Here are a few examples of types we can use:

Public vs Private

You might have seen something like “private float _speed” or “public int health” this has to do with the visibility to other scripts.

Public variables, are variables that are visible to all classes.

Private variables, are variables that are visible only to the class to which they belong.

It is good practice to use private variables as much as possible to make your data less likely to errors and use a function to access this data if you ever need to(more on this later).

Naming Variables

Now our variable needs a name when declaring variables you need to give it a name, this can be whatever you want as long as it is clear to you what it holds.

when naming a variable we use “camel casing” this is the practice of writing phrases without spaces or punctuation, indicating the separation of words with a single capitalized letter.

You might come across variables like int x = 4 or int i = 3, this is used to make the code less heavy when compiling, a downside to this approach is that it can be very difficult when collaborating with others for them to know what this “x” or “i” means so I would advise to give it a short but clear name indicating what the variable is for as to make it more readable for others working on the same project.

Please note a common convention when naming a private variable is to use a _ before the name like_speed or _time so further on in the code you know it is a private variable just by the name without having to look up the actual variable.

In the coming articles, we will use variables more and more since they are at the core of every game or program and the more you use them the more familiar you get with them.

Thank you for reading and hope to see you next time, until then happy coding!