Variables: the building blocks of code. When building a game in Unity, you often need to store information, such as a player’s age or the current time. Variables are perfect for storing data. If you are a beginner and want to learn how to build virtual reality games, check out our Unity3D course!
In this tutorial, we will use an example of a cube to show some of the useful things variables can do. We will change the size, name, and rotation of a cube using variables in C#.
To simplify this example, you can delete the following lines from the C# script we created in the last tutorial.
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
Changing Size
Suppose we want to change the size of the cube we created last time. We can create a variable to store a change in size. In Unity, you can see that the default Scale values of Cube are 1 1 1. In the Cube script, you can modify Cube’s Scale values in the Cube class. Type the following bold line of code in the script:
using UnityEngine;
using System.Collections;
public class Cube : MonoBehaviour {
public float sizeModifier = 2.5f;
}
The preceding code declares a variable named sizeModifier
. In C#, the naming convention for variables is to capitalize the first letter of every word after the first word.
float
means that sizeModifier
stores a floating-point number, also known as a float. Floats are numbers that can contain decimal places.
The public
keyword means that classes other than Cube can access sizeModifier
. As well, using public
allows us to modify sizeModifier
in the Unity editor.
To declare sizeModifier
, we could have just typed public float sizeModifier;
. To initialize the variable (store a value in it), we added = 2.5f
.
=
assigns a value to a variable. The code to the right of the equals sign will be placed in sizeModifier
. 2.5f
stores the value 2.5 as a float.
Save the script. Open Unity. The sizeModifier
variable will be represented as the property “Size Modifier” in the Cube (Script) component. Note that the script must finish compiling before you can see changes.
To change sizeModifier
, you do not need to go to the Cube script, change 2.5
, and wait for another compilation. Instead, you can change the value of Size Modifier in the Inspector. As such, you can test your values faster.
Renaming
Let’s rename Cube. To store Cube’s name, create another variable in the Cube class.
public class Cube : MonoBehaviour {
public float sizeModifier = 2.5f;
public string newName = "";
}
newName
is a variable of type String. A string variable is a collection of characters. The value of a string needs to be enclosed in double quotation marks. Currently, the string is empty. Use the following format to name the cube “Cubey1337”.
public class Cube : MonoBehaviour {
public float sizeModifier = 2.5f;
public string newName = "Cubey1337";
}
Save the script, and open Unity. When the compilation completes, the field “New Name” will appear in the Cube (Script) component. Here you can change the value of newName
, for instance, to “Cubey42”.
Changing Rotation
Another type of variable is the Boolean. A Boolean variable can contain one of two values: true
or false
. Use the following code to create a Boolean variable that determines whether Cube will be rotated.
public class Cube : MonoBehaviour {
public float sizeModifier = 2.5f;
public string newName = "Cubey1337";
public bool isRotated = false;
}
The value of isRotated
is initially false
, which means the cube will not be rotated. Save the script, and open Unity. The property “Is Rotated” will appear in the Cube (Script) component.
Unlike the Size Modifier and New Name properties, which are text fields, Is Rotated is a checkbox. The checkbox is checked when isRotated
is true
and unchecked when isRotated
is false
.
Want to learn more about variables? Check out our Unity3D course, where you build 30 virtual reality games!