C# Variables in 5 Minutes – Unity Game Development Tutorial

Learn a fundamental programming topic and the basics of game development. Start coding your first lines today with this free Mammoth Interactive tutorial.

In this tutorial:

Part 1: Variables for Game Developers
Part 2: Declaring Your First C# Variable
Part 3: C# Integer Variables
Part 4: Performing Operations

Setup

To follow along with this tutorial, you’ll need to open a new Unity project.

We’re using Unity v5.4.3f1. If you’re using a different version of Unity and find an incompatibility, drop us a comment down below with your question.

Part 1: Variables for Game Developers

First up: what the heck are variables? A variable is a container that stores data of a certain type. Without variables, there would be no way to store and reuse different objects in your game and the properties of those objects.

As a game developer, you’ll use variables all throughout the scripts of your game to make references to players, objects and more abstract things like speed and score.

Part 2: Declaring Your First C# Variable

Let’s write our first lines to declare a variable in the script that controls our game. That means the script attached to the Game Controller object. If you don’t have a Game Controller yet, check out our previous tutorial.

Open GameController.cs. In the GameController class, below the class definition and in front of the Start method definition, type the following code.

public string message = "Hi there!";

This line declares a variable with the name message. Note that the C# naming convention for a variable is to capitalize the first letter of every word other than the first word. That’s why the variable name is lowercase.

message will point to a certain position in the game’s memory that will store something. With variables, we do not need to memorize a long number as the address for a part of memory in your device. Instead, we can use a name like message.

The keyword string in this line of code refers to the type of data that is stored in message. message will be of type string.

The keyword public means that other classes can access message. As well, with public, we can edit message in the Unity editor, which we will do later.

We could have just declared the variable with the following line:

public string message;

We didn’t leave the line at that because we wanted to initialize the message right away with a value. The value of the message is the text of the message itself. To give a variable a value, you use an equals sign and put the value to the right of the equals sign. With this, we successfully put the value “Hello there!” into message.

In the Start method, we can call the Debug class with the Log method to print the value of the message variable.

void Start() {
Debug.Log(message);
}

Let’s test this script and see what the Log method will print in the console. It is good practice to save and test code often so that you can catch errors early on.

How the Script Affects the Inspector

Save the file, and open Unity. Select the GameController object. When the compilation is finished, the property “Message” will appear in the Game Controller (Script) component in the Inspector. This property is the message variable we created.

screenshot of the game controller script property in the unity inspector
The variable creates a new property in the Inspector.

Testing a Variable Value

Press the Play button above the main Scene window. “Hi there!” will print in the Console instead of “Hello World!”. Press Stop. In the Inspector, change the value of Message to “Let’s go!”. Press Play. The Console will print “Let’s go!”.

Press Stop. Evidently, we can change values in the Unity editor without having to change our code. This is useful especially in bigger projects where compiling scripts can take a long time.

Part 3: C# Integer Variables

Let’s look at some other types of variables. Open GameController.cs, and type the following code below the message variable.

public int number1 = 5;

This code declares a variable named number1. The variable’s value is 5.

Notice that we didn’t put quotation marks around the value because the variable is not a string. The variable is an integer. Integer variables can store numbers without decimal places.

Let’s test this variable’s value just like we did for the string variable. In the Start method, call the Log method a second time. Pass number1 as the method’s parameter.

void Start() {
Debug.Log(message);
Debug.Log(number1);
}

To test the code, save the script. Open Unity, and press Play. The Console will print the messages “Let’s go!” and “5”.

Even though number1 is not a string, its value is converted into text so that we can see it in the console.

Press Stop.

Part 4: Performing Operations

We can even perform operations on number1. Let’s see how!

Open GameController.cs. Create an integer variable named number2. Give it the value 37.

public int number2 = 37;

Performing Addition With Variables In C#

In the Start method, instead of number1, we can print the sum of number1 and number2. Use the following format to do so.

Debug.Log("The sum is: " + (number1 + number2));

The first + operator adds number1 and number2. The second + appends (in other words, attaches) the string “The sum is ” to the sum. The operator is called adding when it is with numbers, and called appending when it includes srings.

Because the two numbers are integers, the sum will also be an integer.

Let’s test this line of code! Save the script, and open Unity. Press Play. The Console will print “The sum is 42”.

That does it for this tutorial on C# variables! Stop the scene, and don’t forget to save the project.

Review

Part 1: Variables for Game Developers
Part 2: Declaring Your First C# Variable
Part 3: C# Integer Variables
Part 4: Performing Operations

—Team Mammoth from Mammoth Interactive INC. Tutorial by Glauco Pires and Transcribing by Alexandra Kropova

Liked this tutorial? Get a free game development video course!

More resources on this topic:

One Reply to “C# Variables in 5 Minutes – Unity Game Development Tutorial”

Leave a Reply

Your email address will not be published. Required fields are marked *