Declaring Variables in Unity with C#
What is a Variable?
In programming, a variable is a container that holds a value. These values can be of different types, such as numbers, text, or even complex objects. Recording the player’s health, score, ammo, position in the world, current weapon etc. are all vital to game logic and flow, and therefore we need to use variables constantly. We’ve covered these before in Construct 3, where we added instance or global variables to track the game logic. The word variable is also quite specific, as it means that the information can change. The player’s level is a great example, as you are able to level up, changing the number.
Declaring Variables
Declaring a variable means telling the computer what type of data the variable will store and giving it a name. In Unity, we are essentially building components (like behaviours in Construct 3) and declaring that the component will have this data attached to it. In C#, the syntax for declaring a variable is:
DataType: The type of data the variable will store (e.g., int for integers, float for floating-point numbers, string for text).
variableName: The name you give to the variable. This should explain what the variable will be used for to someone who didn’t write the code. Additionally, it needs to be a series of letters without any spaces, with each new word signified with a capital letter at the beginning, e.g. playerScore. The first letter is not capitalised. This is known as ‘camel-case’.
Semi-colon: ( ; ) This is used as a statement terminator. Think of it as a substitute for a full-stop, because the full-stop is already used for something else in general programming syntax.
Examples:
But what are these data types?
Variable Types
int
(Integer): int is short for integer. It represents whole numbers without any decimal points. In Unity it might be used for storing values like player score, number of lives, or level index.
float
(Floating-Point): float represents numbers with decimal points. It's commonly used for variables that require fractional precision. In Unity it might be used for storing values like speed, time, or health points with decimal values.
string
: string is used to represent sequences of characters, typically used for text. In Unity it might be used for storing values like player names, dialogue text, or any textual information.
bool
(Boolean): bool represents a binary value, either true or false. It's used for logical operations and conditions. In Unity it might be used for storing values like game state (e.g., is the game over?).
Initialising Variables
After declaring a variable, you can assign an initial value to it. This is called initialization. The syntax is:
Examples:
Specifically then, the format of each needs to be:
For
int
, simply a whole number with no decimal place.For
float
, we need a number, with a decimal point, and the letter f at the end.For
string
, we need some characters enclosed in double-quotation marks “ ”For
bool
, simply the word true or false, all lower-case.
Tips:
Choose meaningful variable names for clarity (e.g.,
playerScore
instead ofps
).Follow C# naming conventions (
camelCase
for variables).Initialise variables with appropriate values when possible.
Last updated