- Learning C# by Developing Games with Unity 5.x(Second Edition)
- Greg Lukosek
- 1303字
- 2021-07-09 20:03:32
Understanding what a variable is and what it does
What is a variable? Technically, it's a tiny section of your computer's memory that will hold any information that you put there. While a game is running, it keeps track of where the information is stored, the value kept there, and the type of that value. However, for this chapter, all you need to know is how a variable works. It's very simple.
What's usually in a mailbox, besides air? Well, usually there's nothing, but occasionally there is something in it. Sometimes, there are letters, bills, a spider, and so on. The point is that what is in a mailbox can vary. Therefore, let's call each mailbox a variable.
In the game development world, some simple examples of variables might be:
playerName
playerScore
highestScore
Naming a variable
Using the example of the mailbox, if I asked you to see what is in the mailbox, the first thing you'd ask is, "Which one?" If I say in the Smith mailbox, the brown mailbox, or the round mailbox, you'll know exactly which mailbox to open to retrieve what is inside it. Similarly, in scripts, you have to give your variables a unique name. Then I can ask you what's in the variable named myNumber
, or whatever cool name you might use.
Note
The golden rule
When you name variables, try to come up with the best name that accurately describes what value your variable contains. Avoid generic names such as name
, speed
, and score
. Instead, name them playerName
, carSpeed
, and opponentScore
, respectively.
A variable name is just a substitute for a value
As you write a script and create a variable, you are simply creating a placeholder or a substitute for the actual information that you want to use. Look at the following simple math equation: 2 + 9 = 11.
Simple enough! Now try the following equation: 11 + myNumber = ???. There is no answer to this. You can't add a number and a word. Going back to the mailbox analogy, write the number 9
on a piece of paper. Put it in the mailbox named myNumber
. Now you can solve the equation. What's the value in myNumber
? The value is 9
. So now the equation looks normal: 11 + 9 = 20.
The myNumber
variable is nothing more than a named placeholder that can store some data (information). So, wherever you would like the number 9
to appear in your script, just write myNumber
, and the number 9
will be substituted.
Although this example might seem silly at first, variables can store all kinds of data that is much more complex than a simple number. This is just a simple example that shows you how a variable works. We will definitely look at more complex variable types at later stages. Remember, slow, steady progress, baby steps!
Creating a variable and seeing how it works
Let's see how this actually works in our script. Don't be concerned about the details of how to write this; just make sure that your script is the same as the script shown in the next screenshot:
- In the Unity Project panel, double-click on
LearningScript
. The MonoDevelop window should open automatically onLearningScript.cs
. - In MonoDevelop, write the lines 6, 11, and 13 shown in the following screenshot:
- Save the file.
Note
The best way to save your script is by using a shortcut. If you are using Mac, use command + S, and on Windows use Ctrl +S. We will be saving a new version of the script every time some changes are made to it, so it is a good idea to use a shortcut instead of saving through the File menu.
We have added a few lines to our script. Before we check whether it works or what it actually does, let's go through line 6:
public int myNumber = 9;
In simple words, this line declares a new number type variable named myNumber
and assigns a value of 9
to it. We don't want to worry about theory too much now and want to write more code, right? Agreed, but we do need to remember a few things first.
Declaration
To create a new variable, we first need to declare it by saying what type of variable it is. In this case, we want to create a number variable. The keyword for whole number variables in C# is int
. We also have to give our variable a name; myNumber
is fine for now. You can use any name you want as long as it does not contain spaces and special characters.
Assignment
We have created our variable, and now we are giving it a value. To assign a value, we use the equal to sign followed by the value. In this case, it is 9
. To close the line, use a semicolon.
Click on Play!
Quite an exciting moment! Go back from MonoDevelop to Unity, and click the Play button. Unity should print out two lines on the Console tab, looking like this:

Unity executed the code in the LearningScript
component on GameObject
just after you clicked on Play. We can see two lines printed on the Console window. We wrote a piece of code asking Unity to print these two values on the Console window. Let's look again at lines 11 and 13. Everything inside the brackets in the Debug.Log
function will be printed to the Unity Console. It can be a number, text, or even an equation.

So, line 11 is asking, "Hey Unity, print the result of 2 + 9 on the console!" Line 13 is using the myNumber
variable's value directly and adding it to the number 11
.
Thus, the point of this exercise is to demonstrate that you can store and use whatever values you want using variables and use their name directly to perform operations.
Changing variables
Since myNumber
is a variable, the value that it stores can vary. If we change what is stored in it, the answer to the equation will also change. Follow the ensuing steps:
- Stop Unity by pressing the Stop button and change
9
to19
in the Unity Inspector tab. - Notice that when you restart the game, the answer will be
30
.
I bet you have noticed the public
keyword at the very beginning of the line that declares the myNumber
variable. Let me explain what it means. It's called an access modifier. We use these to specify the accessibility of a variable. The public
keyword means that the variable can be seen by code outside our script. Look again at the Unity Inspector tab. You can see the value of myNumber
there because it is public. The private keyword, however, means that the variable can be accessed only by code in the same class.
Note
Private variables are not visible in the Unity Inspector tab. If you wish to control or view them, make them public.
Watching for a possible gotcha when using public variables
Unity gives us great flexibility with editing or reading public variables in the Inspector tab. You will be using public variables most of the time. Now, I want to make you aware of something that might give you a headache sometimes.
Note
All public variable values are overridden by the Unity Inspector tab.
Let's look back at line 6; we had assigned our variable a value of 9
. This value will be copied to Unity Inspector. From now on, the value from Inspector is taken in to account and not the value in the script, even if you change it. Therefore, be careful as this is very easy to forget.
In the Inspector panel, try changing the value of myNumber
to some other value, even a negative value. Notice the change in the answer in the Console tab.