Basics of C# and Unity - Variables

What is a variable?

You can imagine a variable as a container that has some sort of information stored in it. This container is called a ‘Variable,’ and the information is called ‘Data.’ Variables allow us to store essential data throughout the program. There are two main types of variables in C#:

Value Types

Variable of value types contain an instance of the type. They are stored in a pre-defined location in the memory called the Stack. The most used value data types are:

Reference Types

Variables of reference types store references to an instance of the type. As their name suggests, reference types hold a reference that points to their value, which is stored on the Heap. Reference Types have a default value of ‘null.’ They are, for example:

The main difference between the types

  • With reference types, two variables can reference the same object; therefore, operations on one variable can affect the object referenced by the other variable.
  • With value types, each variable has its own copy of the data (they directly contain their data). It is not possible for operations on one variable to affect the other.

How to define a variable in C#

There are three(two*) required components and one optional to create a variable. They are ‘Access Modifier,’ ‘Data Type*,’Variable Name*’ and the optional one is ‘Value.’ Although Optional, every variable has a default value.

The syntax to declare a variable is as follows:

Variable Name - The name of your variable.

Access Modifier - Determines who and what has access to your variable. The most common modifiers are ‘public,’ ‘private,’ and ‘protected.’

Public - Variable is accessible to Any other class that references it, even the Unity editor.

Private - Variable is not accessible by any other Method or Class, not even the Unity editor.

Protected - Variable is only accessible by the class they belong to and it’s subclasses.

Data Type - Determines the type and the storage size of data your variable will contain. To learn more about each Data Type, click here.

When creating private variables, it is common practice to start the variableName with a ‘_’ - after that, you do not have to type in the ‘private’ access modifier.

If you want to have a private variable - but you still want to be able to change its value at run time in the editor. You can do this by Serializing the variable, and you will be able to change the value in the ‘Inspector.’

This is something we have done in the Previous Article.

Type conversion and Casting in C#

In C#, types can also be converted to other types. This is called ‘Casting.’

There are three main sorts of ‘Casting.’

Implicit conversions - This is done with no special syntax, there is no data loss involved

Explicit conversions - This type of casting requires a ‘Cast Expression’ to avoid loss of data.

Conversions via Methods - These are Methods that are being used when converting between types, which are not compatible.

You should now have a basic understanding of C# Variables! Of course, there is still more to learn about variables, and if you wish to know more - you can head over to the official documentation!

But that is it for now, thank you for reading and feel free to follow me for more articles — and as always, good luck and see you next time!

Aspiring and eager beginner Unity developer