Defining Variables — Unity C#

Imran Momin
4 min readApr 9, 2021

--

Let’s start with a simple question: what is a variable? Depending on your point of view, there are a few different ways of answering that question:

· Conceptually, a variable is the most basic unit of programming, as an atom is to the physical world (excepting string theory). Everything starts with variables, and programs can’t exist without them.

· Technically, a variable is a tiny section of your computer’s memory that holds an assigned value. Every variable keeps track of where its information is stored (that is called a memory address), its value, and its type (for instance, numbers, words, or lists).

· Practically, a variable is a container. You can create new ones at will, fill them with stuff, move them around, change what they are holding, and reference them as needed. They can even be empty and still be useful.

A practical real-life example of a variable is a mailbox; remember those?

They can hold letters, bills, a picture from your aunt Mable — anything. The point is that what’s in a mailbox can vary: they can have names, hold information (physical mail), and their contents can even be changed if you have the right security clearance.

Names are important

Referring to the preceding photo, if I asked you to go over and open the mailbox, the first thing you’d probably ask is: which one? If I said the Smith family mailbox, or the brown mailbox, or the round mailbox, then you’d have the necessary context to open the mailbox I was referencing. Similarly, when you are creating variables, you have to give them unique names that you can reference later.

Variables act as placeholders

When you create and name a variable, you’re creating a placeholder for the value that you want to store. Let’s take the following simple math equation as an example:

4 + 9 = 13

Okay, no mystery here, but what if we wanted the number 9 to be its variable? Consider the following code block:

myVariable = 9

Now we can use the variable name, myVariable, as a substitute for 9 anywhere we need it:

4 + myVariable = 13

Even though this example isn’t real C# code, it illustrates the power of variables and their use as placeholder references.

Creating a variable

Alright, enough theory; let’s create a real variable in our script:

1. Create a script in Unity, and name it “LearningCurve”.

2. Double-click on the script to open it and add lines 7, 12, and 14 (don’t worry about the syntax right now — just make sure your script is the same as the script that is shown in the following screenshot):

3. Save the file.

For the scripts to run in Unity, they have to be attached to GameObjects in the scene. Let’s attach the LearningCurve script to the Main Camera.

  1. Drag and drop LearningCurve.cs onto the Main Camera.
  2. Select the Main Camera so that it appears in the Inspector panel, and verify that the LearningCurve.cs (Script) component is attached properly.
  3. Click Play and watch for the output in the Console panel.

The Debug.Log() statements printed out the results of the simple math equations we put in between the parentheses. As you can see in the following Console panel screenshot, the equation that used our variable worked the same as if it was a real number.

Changing a variable’s value

Since currentAge was declared as a variable on line7, the value it stores can be changed. The updated value will then trickle down to wherever the variable is used in the code; let’s see this in action:

  1. Stop the game by clicking the Play button if the scene is still running.
  2. Change currentAge to 25 in the Inspector panel and Play the scene again, looking at the new output in the Console panel:

The first output will still be 30, but the second output is now 26 because we change the value of our variable.

The goal here wasn’t to go over variable syntax but to show how variables act as containers that can be created once and referenced elsewhere.

--

--

Imran Momin
Imran Momin

Written by Imran Momin

A VR/AR developer, who enjoys making games and developing interactive environments using Unity’s XR integration toolkit for Oculus quest and HTC vive devices.