Writing Your First Variable

David Hunter Thornton
4 min readAug 6, 2022

--

What is a variable and how does it work?

Objective: Explain the different types of variables and how this helps your current project.

In my last article we set up our code to allow us to directly manipulate the speed an object moves. There’s just one problem, we shouldn’t have to access the code every single time we want to change that number. So instead, let’s change it to a variable.

Think of a variable as a box, or container of information. This box can have other variables or even directions to a different variable or function/method inside it.

These variables have a few different labels to keep them organized. The first of these is “public” or “private”. Public variables can be accessed by anything else in the project. Any other script can “talk” with your script and find the information inside that variable. Private variables are only accessible by that script itself.

The next type of identifier for variables has a few different names.
int (integer) = for any whole number
float = for any decimal number (which using math we can translate percent and/or fractions to work with this as well)
bool (boolean) = for true/false statements
string = for words/letters

The very last identifier/label for each variable is it MUST have a unique name. So think of the first two labels as a house, one with a locked door, and one without. The next label tells you which room the box goes in. And finally, what we wrote on the outside of the box to know what’s inside it.

There’s one final thing that is optional and its that you can declare a value for your variable to have. For instance we can say “This variable is equal to 4 of something”. Maybe even 4 meters per second?

If you know, you know.

With all of this new knowledge about variables, let’s actually put it into practice with our code from the last article.

Now, an extra note that you should be aware of. The only difference for what I’ve written above, and any of the other data types, is for the “float” variables. For that, you need to include a lowercase “f” after the number and before the semi-colon. Like so:

Now comes the best part, and the whole point of this article. We can input that new variable of “speed” in place of the hard coded “5” at the bottom. This will change the ‘math’ to include whatever we’ve set the value of “speed”.

If we save that new code, and let Unity compile it, you’ll see a new section in the Inspector.

This is one of the things that makes variables so powerful. If you make a variable public, you can edit the value assigned in the Unity Inspector. The Inspector will always override what’s in the code as well.

You see how even though I have the code written as “translate.up” making the speed variable negative still causes it to go down? This is because 1 meter per second times -1 is going to equal -1! You’ll find that lots of coding practices directly correlate with math.

I know this was a text heavy section and that we covered a lot of info. Hang in there, the more you do it, the easier it’ll come to you.

Friendly Reminder: Don’t forget to keep updating your project through GitHub/Git and saving any changes you make to a separate branch. You can check out my other articles to get a breakdown of those steps! Day 1–9

--

--