Basic Building Blocks — Variables.

Michel Besnard
4 min readApr 20, 2021

--

www.boxed-up.co.uk

A variable can be seen as a container that stores information.

The developer creates this box and uses it as a way to store some data. Typically, this data will vary, hence the term variable. There are three components to a variable, with an optional fourth.

Reference Data Type Name = Value

Reference

There are two References: Public and Private.

These containers can be referred to as Public, which means that they can be seen in the Unity editor, by everyone and shared all over the place. This also permits you to change the data associated with the variable right in the editors’ Inspector window. Please note that if you change the information in the Inspector window, it will override the original data of the variable in the associated script.

They can also be Private. Private signifies that access to the stored data is restricted, sort of hidden behind a curtain, meant for a specific set of instructions to use (think script). They are usually hidden in the Unity editor as well, but as we’ll see later, there is a way to reveal them in order to allow developers to play with the variables without needing to access them via their associated script. It may still change (vary), or it can be a fixed value.

Data Type

There are four commonly used Data Types: String, Int, Float, and Bool.

You will use Strings to express characters of text, such as names, sentences, etc. These have to be written between quotations. An example of this would be:

Int stands for integers and represents whole numbers. A Float will equate to a decimal number. Both Int and Float can be either negative or positive. Examples would be:

And finally, the Bool is a boolean value. Think of a Bool as an on/off switch. It’s either true or false, black or white, 1 or 0. The default value, if left blank, is “false”. You would write the following:

Please note the following conventions in the bool example above:

The first one is called camelCase, where the letter of the first word in the name is lower case, while the first letter of every subsequent word is capitalized.

The other convention is to add a “_” before private variable names.

You can read further on C# data types at the following link:

Name

Every box, every container needs a name. Without a logical descriptive name, all these boxes would look the same. The name should define what the variable is used for like “health”, “speed” or “ammo”.

Value

This component is optional and can be defined later in your script, or Unity will simply assign a default value like “0” or “True”.

Some common best practices for variables

In order to quickly determine if a variable is public or private, use an underscore as discussed earlier for private variables:

If you omit the private at the beginning, then the variable will default to private, so:…

…is the same as…

The standard is that all variables should be private unless they need to be accessed by other scripts, then make them public.

As I hinted earlier, in order to be able to view a private variable in the Inspector, use [SerializeField] like so:

And there you have it. I hope you’ve enjoyed this short article on variables. See you next time!

--

--

Michel Besnard
Michel Besnard

Written by Michel Besnard

Military member with 35+ years of service, undertaking an apprenticeship with GameDevHQ with the objective of developing solid software engineering skills.

No responses yet