Variables in C# Unity

Variables are the building blocks of programming with C# in Unity.

A Variable is like a box containing information by the hand of parameters that you set up.

A variable can either be public or private.

-public variables are accessible by any class and are visible for everyone in the inspector.
-private variables are only accessible by referencing through this class or struct.

There are 2 main types of variables that are used in C# Unity.

— Value Types, variables that are stored in a pre-defined location in memory.

Some examples are:
int stores integer values (whole numbers) = 1, 4, 52, 120.
float stores floating point numbers = 1.2, 2.05, 321.28.
bool stores a logical Boolean type that is either “true” or “false”.
char stores a single character value = “A”.

— Reference Types, which have a default value of null and are stored differently than value types.

Some examples are:
string references a sequence of characters like “PlayerName”, “AmmoType”,…
Object references to the base class for all other Types.
Array that can hold a number of Objects by usage of a Data structure.

To define a variable:

Examples in our script:

To prevent loss of data, Types can also be converted to other Types but that does not apply for our script yet. You can find more information here, at the Microsoft documentation.

Previous | Next

Software Engineer — Unity Game and Application Developer