Learning Unity — Variables: The Most Basic Element of Scripting

Joshua Nielsen
4 min readJun 1, 2021

If you’re going to want to make games in Unity, you’re going to want to learn scripting. And if you’re going to learn scripting, the most basic concept you need to learn is variables.

Variables are how we store and manipulate information we need in our game. For example, you may have a variable to represent the player’s score, whether or not an enemy is aware of the player, or the amount of fuel in an in-game vehicle. In this article, we are going to discuss variables in Unity scripts. To begin, let’s look at the four basic data types of Unity scripting — int, float, string, and bool.

The int (also known as integer) data type is used for whole numbers, either positive or negative. In a game setting you would use this data type for any value that would not have fraction values. For example, you might use an int to represent the player’s remaining lives or the amount of ammunition they have.

The float (also known as floating point) data type is used for numbers that could have a fractional component. You might use a float to precisely represent a measure of in-game distance or time.

The string data type is used to store a sequence of characters. In other words, anything you can write can be a string. A string might be used to store the name or description of an item or character in a game.

A string could contain a number, or even be nothing but a number, but it does not have any mathematical meaning to the script when it is a string. For example, if you had one string that contained the number “1” and another that contained the number “2”, you could not add them together to get 3. If you need to perform mathematical operations on your data, you need to use an int or float.

The final data type, bool (also known as boolean), is used for data that only has two possible values. In Unity scripts, those values are true or false. In games, this can be used to represent all sorts of things, such as whether a certain power or ability is currently active or if the player has completed a certain quest.

Variables are created by declaring the data type and giving it a name. For example, if I wanted to create a string and name it “exampleString”, I would enter the following:

Creating a new string variable

Notice also the semicolon at the end of the line. If you’re new to programming/scripting, you will find that most lines of code in a Unity script need to end with a semicolon. This is because Unity uses the C# language as its scripting language.

So now that you have a string variable, what can you do with it? Currently it’s just an empty variable, so not much at the moment. We need to put an actual string into the string variable so it can remember it for us. We do this by assigning the variable a value. This is done by using the equals sign — “=”.

Assigning a value to our string variable

When we do assign a value to a variable, it is necessary that the value matches what the variable expects. If it does not, Unity will give you an error and your code will not run. For example, you cannot assign a value of true to an int or “Hello world” to a float. Also, any string value must be enclosed in quotation marks, otherwise the software will not recognize that it is meant to be a string.

An invalid string value. The red lines from the code editor indicate that there is an error.

One other thing to note is that a float variable expects numbers assigned to it to be followed by an “f” character. This is because floating point numbers in C# are typically of the type double. This is not important to your Unity scripting at the moment, but if you are curious feel free to investigate the distinction between the two types.

You can also both declare a variable and assign it a value on the same line of code. See some examples here:

Declaring variables and assigning values to them

So that concludes our very basic introduction to variables in Unity. I hope that is helpful in getting you started on your path to game development. Good luck and happy coding!

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

No responses yet