Variables — The Building Blocks Of Programming

I love Lego! Are variables actually like building blocks?

Variables are an element of something that can change. We all have birthday’s right? Every year our age changes. That’s a variable!

In Unity, we have to create everything ourselves. A character’s age doesn’t exist unless we add it. Poor Minecraft Steve might not get a birthday this year. So how do we code it in?

Firstly we have four types of variables we can create. There are some others, but these are the main ones:

Int: An Integer is simply a whole number. Great for someone’s age.

Float: A Float is a number containing floating points or more commonly decimal values.

String: A String is used to store words. Great for someone’s name.

Bool: A simple true or false question. Great for storing yes or no conditions.

After declaring what type of variable we want to make. We need to give it a name, in Unity the commonly used naming convention is camelCase. It’s the practice of writing words compounded together, using a capital letter to define each new word, and commonly begins with a lowercase letter.

For example, string myName, or, int myAge.

We can also choose to give our variable a value in our code. Or alternatively we could do this from the inspector. This is where variable access is important.

If we want a variable to be accessed by other scripts in our game, we can make it public. This would also allow us to edit our values in the inspector, making for easy adjusting and playtesting. But some variables we want to be confined to it’s own script. Therefore we make our variables private. This is also the default configuration for every new variable.

We can see our private variables in the inspector by using [SerializeField] in the line above.

It’s also worth noting that changing the value of your variables in the inspector over writes what is within the script. But if in playtesting mode, the value will resort back to what it was before hitting play.

Here’s some examples before moving on:

Variables aren’t just manipulated by the inspector. We can also use code to apply arithmetic or change the value of our variables entirely. We could use a bool to check ifMarried, and upon becoming true, myLastName could change.

Or we can add a year to myAge variable when the date hits my birthday. This would look like myAge += 1, adding one to the total.

Maybe we could even find the average of a set of numbers with a variable such as score.

So just like building blocks, the possibilities for variables are endless!




Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Using OpenResty in enterprise grade environments

OpenResty logo

Resolve conflicts in GIT working with branches correctly

5 Ways a CMMS Solution Streamlines Maintenance Work Order Management

Devops videos- 29 février 2020 | xavki

Adapting Agile for a new industry

Made With Unity | 2D Space Shooter Part 15: Game Audio

Point in time documentation of the Keybase Chat API

Docker tutorial — Create,Add Nodes,Deploy and Scale service on the Swarm

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Calum Slee

Calum Slee

More from Medium

How to use Post Processing in Unity

Overloading methods in C# & Unity

Cooldown System in Unity with C#

Voiceover Event Calls in Unity