Dev Day 13: Variables! — The Building Blocks of Programming

In the previous article, Simple Movement in Unity3D Part 2 (the link is at the bottom of this article), we started working with variables within our C# script. For instance, when we wanted to control and dictate the rate of speed that our Player GameObject was moving at across the screen, we created a variable called _speed to hold that data. Without this data, the computer has no idea what the speed of our GameOject is on its own, so we need to define what speed is in our game using a variable.

Variables are containers that store information about our game. This information can be anything from what the player’s health is, assigning an image icon for an asset, or even creating the logic to know whether an item has been picked up. Every detail about how our game is played will be stored within variables.

Looking back at our _speed variable, let’s break down what is happening here. First, a variable must be declared within a class, and we are declaring _speed within the Player class. Then we need to decide whether our variable will be public or private. The difference between a public variable and a private variable is that the information stored within a public variable can be accessed by other scripts and is also directly editable within Unity3D, where as the information stored within a private variable is only accessible within its defining script and is not directly editable or visible within Unity3D.

Instances where we would want a public variable is for things like a character’s name being available to be used in dialogues, or if other players need to see another player’s health information. However, most of the time, we would be using private variables. The issue with private variables is that by themselves they are not editable within Unity3D, and that can create an issue where we would want to be able to adjust the information in Unity3D. Fortunately, by utilizing [SerializeField] attribute ahead of the variable declaration, the private variable will be visible and editable within Unity3D while remaining private.

Going back to our _speed example, we can see that _speed is a private variable but is also editable in Unity3D because we have utilized the [SerializeField] attribute. Another identifier that _speed is a private variable is the use of the “_” to prefix the variable name. All private variables in C# must begin with an “_”. This helps to identify whether a variable is private or not without having to reference the initial variable declaration.

This moves us to another important part of declaring a variable, all variables must have a name. It is best to use a name that best describes what the variable is or is doing. Variable names also use camel casing for when a variable uses more than one word for the name. An example of this is if we wanted a variable to store the player’s name, we would call the variable playerName, and if we wanted it to be a private variable, we would call it _playerName.

So, now that we have the name of the variable and whether it is public or private, we need to determine what type of data we will be storing within our variable. Again, all information about our game will be stored within variables and these variables will contain information such as numbers, names, and true or false references, just to name a few, and we need to define this information within the variable. Luckily, there are predefined Datatypes that we will use to declare the type of information within our variable. Here is a list of the most used C# Datatypes:

int (integer) = whole numbers, Ex: 5

float = decimal numbers, Ex: 10.52f (must be suffixed by an f)

bool (Booleans) = true or false (false by default)

string = text characters, Ex: “Hello World!”

char (character) = individual text character, Ex: ‘A’

There is also a great reference for even more C# Datatypes here: https://www.tutorialsteacher.com/csharp/csharp-data-types

Referencing our _speed variable, we can see that we are utilizing a float Datatype instead of an int Datatype because the information will be incremented at smaller values than a whole number. It is also important to use an f after the decimal value when declaring a float Datatype.

However, we do not always need to declare what the value of the Datatype is within the initial variable declaration. We can declare the value of the Datatype later within a Method or even update the declared value to a different value.

So, these are the basics when it comes to variables and to recap, variables need to be declared within a class, need to be private or public, the Datatype must be declared with an optional value, and the variable must have a name.

Reference: Simple Movement in Unity3D Part 2: https://petethomasberger.medium.com/dev-day-11-simple-player-movement-in-unity-part-2-e2f190ba1b39