A programming language like C# can appear to be very complicated at first, but in reality, there are two basic parts that form its foundation. These parts are variables and methods. Therefore, understanding these critical parts is very necessary for learning any of the other features of C#. As critical as they are, they are very simple concepts to understand. Using these variable and method foundation pieces, we'll introduce the C# building blocks that are used to create Unity scripts.
For those who get sweaty palms by just thinking of the word script, wipe your hands and relax! In this chapter, I'm going to use terms that are already familiar to you to introduce the building blocks of programming. The following are the concepts introduced in this chapter:
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
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.
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!
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:
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.
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.
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.
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:
9
to 19
in the Unity Inspector tab.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.
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.
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.