“Variables! - The Building Blocks of Programming”

A definition of the word “variable” is something capable of being varied or changed. In software development, the term “variable” refers to an allocated position in memory that has a label and can be altered by other processes.

In this article, we will briefly discuss the use of variables within the Unity development environment. A variable has four main attributes.

  1. Your variable has to have a name. This name should be logical and define the variable. To avoid spaces in the names assigned to variables we use a format called “Camel Case”. If the name is made up of more than one word we eliminate the spaces and capitalize starting with the second word and every word after that. For example: “bool playSuspenseLoop1;” or “int myAge”
  2. The “variable” can be declared as “public” or “private”. A “public” variable can be accessed by all parts of the program. A “private” variable can only be accessed from within the class it was declared. A “public” variable will be visible in the “Inspector” window by default and can be changed during runtime. If a variable is not declared as a “private” or “public”, it will become a “private” variable by default. A “private” variable, by default, will not be visible in the “Inspector” window unless we place [SerializedField] before the variable’s declaration. So if we create “private int damage;” it will not be visible in the “Inspector” window.

[SerializedField] is a Unity attribute that allows access to the private variable (“damage”) in the “Inspector” window while still keeping it private.

3. There are four main “Data Types”.

“bool”- represents “TRUE” or “FALSE”, “0” or “1”.

“int”- An integer is a whole or non-decimal number. Its range is -2,147,483,648 to 2,147,483,647. There is an integer known as a “short” which has a range of –32,768 to 32,767.

“float”- The floating point or real number can contain a decimal point. Float values in Unity need to have an “f” as a suffix after the number (23.56f). The range of the “float” variable is 1.175494351 E to 3.402823466 E + 38.

“string”- A “string” is a set of characters inclosed by “double quotes”; for example: “Hello World”. We use the “string” variable to store words and names.

4. The variable can be declared with value as an option. For example: or

This completes my article on variables. Until next time…