My journey becoming a Unity game developer: Variables-the building blocks of programming

Objective: Demonstrating the different variations of variables that can be used in a C# program. Understanding the structure that make up a variable including Access Modifiers, Types, variable’s name, and values. Showing how Unity recognizes variables and allow other developers to access and modify the values of the variables.

Different types of variables demonstrated.

What is a variable?

Well, there are a couple of ways to answer this with the 1st answer being a variable is the most basic part of programming where everything starts with them, and without them programs can’t exist.

Another answer is a variable is a small section of your computer’s memory holding an assigned value. Every variable keeps track of where its information is stored through a memory address, its value assigned, and its Type assigned which can be an instance, numbers, words, or lists.

Third answer can be a variable is like a container where you can create new ones, fill them with information, move them around in the program, change what they’re holding, and reference them as needed. Plus, they can be useful even if they’re empty.

When creating a variable they must meet the requirements of:

DataType uniqueName = value;

DataType is the type of data the variable will store and must be specified. UniqueName must be the name of the variable, and different from keywords used in C# to prevent conflicts. Value must match the specified Type if there is a value assigned. Finally, the variable declaration must end with a semicolon to complete the statement. Example: int currentAge = 21;

We can also use ( int currentAge; ) which considered Type-only Declaration as this declaration still follow the rules for variables. We declared the DataType and an uniqueName, but since we didn’t declare a value C# will assign a default value of zero to any undefined values like this variable.

We can also add a security setting to the variable, determining who and what can access the variable’s information through the use of Access Modifiers.

accessModifier dataType uniqueName = value;

There are 4 main Access Modifiers used in C#, but the 2 beginners will use most often in Unity are Public which is available to any script without restriction, and Private which is only available in the class they’re created in called the Containing Class. Also, any variable without an access modifier defaults to private.

The other 2 Access Modifiers are more advanced named Protected which is accessible from their containing class or types derived from it using Inheritance. The other is Internal which can be accessed by any code in the current assembly, but not from another assembly.

Examples: (private int currentAge = 21;) (public int currentAge = 21)

There are 4 common Data Types options used and the values they store are:

int- which is a simple integer (ex. 8)

float- is a number with a decimal (ex. 3.14)

string- characters inside double quotes (ex. “This is it!”)

bool- a boolean decision, either True or False

Another thing we can do with variables is using Type Conversions on them. There are 2 types of conversions with Implicit conversions taking place automatically like when a smaller value will fit into another variable type without any rounding. (ex. float implicitConversion = 5;)

Explicit conversion is needed when there is a risk of losing a variable’s information during the conversion. If we wanted to convert a double into an int, we would have to explicitly convert it by adding the destination type in parentheses before the value we want to convert. This tells the compiler that we are aware that the precision of our data might be lost. In this Explicit conversion, 3.14 would be rounded down to 3 losing the decimal values: (ex. int explicitConversion = (int)3.14;)

One other thing we can use is an Inferred declaration which means you have data you’re manipulating, but you’re not sure what Type it will take. C# can infer a variable’s Type from the assigned value using the VAR keyword. We can use the VAR keyword in the Type’s position and C# will figure out the Type through the value assigned. (ex. var currentAge=21;)

Naming variables should have clear and consistent naming conventions in your code that will not only make it more readable, but will also ensure that other developers on your team understand the intentions of the variable without asking. There are 2 rules you must follow when it comes to naming a variable is that the name you give it should be meaningful, and the other rule is that you use camel case in spelling the variable’s name. Example of naming a variable (public int health = 100;) is ambiguous because does it mean maximum, minimum, or current health. Instead, it should have a name like this (public int minHealth = 0; public int maxHealth = 100; public int currentHealth = 50;). Now other developers on your team will understand what each variable means.

There is something else we need to understand and that is a Variable Scope. Similar to access modifiers which determine which other classes can grab a variable’s information, the variable scope describes where a given variable exists and its access point within its containing class. There are 3 main levels of a variable scope in C# which are:

  • Global — refers to a variable that can be accessed by an entire program which in this case is the game.
  • Class or Member — refers to a variable that is accessible anywhere within its containing class.
  • Local — refers to a variable that is only accessible inside the specific block of code it was created in.

Here are examples of different variables used within a program with Class scope, Local scope, Type-only declaration, Inferred declaration, Implicit Conversion, and Explicit Conversion.

Notice the public variables appears in Unity’s Inspector in the Variables Test script component. These public variables allows developers on your team to test out different values by changing them in the Inspector before and while playing the game. The private variables will not appear in the Inspector because we don’t want their values changed outside of the script. To use private variables in the Inspector, we would have to use an Unity Attribute such as SerializeField.

--

--

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