Existential crisis, what am I? — A quick guide to variables in Unity.
What is speed? What is a name? What is health?
None of these things exists out of the box in Unity. We create them, we name and we use them. They are placeholders for our data.
When we create a variable we need to determine if it is public or private, what data type is it, what we are going to call it and does it have a default value?
Public vs Private
Public
Public variables are accessible and editable from any script in your program. This can be tempting to use for all your variables, but this is bad practice. Why should an enemy have access to change your ammo count? Or why should the player be able to change the enemy's name?
Better use could be the player's health. Let's say an enemy damages the player. The enemy can directly manipulate the player's health according to the damage they caused. The UI Manager can pull the player health directly and update the text on the screen.
Private
Private variables are variables only accessible from the script they are based on. This can prevent issues later in your project so you don’t accidentally change a value of a variable when you were meant to a function on that object.
Let’s say your player has a TakeDamage() function. This function removes health from the player, shakes the screen and provides a red overlay on the camera that gets darker with lower health. In this scenario you would not want the enemy to directly edit the health value of the player, you want to force them to call the public method TakeDamage().
A downside to having a variable as private means you cannot assign a value, nor can you see it in the inspector. To get around this limitation you can serialize the field (using [Serialize Field]).
Common Data Types
There are generally 4 common data types we use in Unity. They have limitations in their use which we will go over later.
String
“This is a string”. “This is our 2nd string”.
When you use the string variable you are telling the program to treat everything within the string as text. You are storing the variable as basically a sequence of characters.
Bool
A bool is a simple variable. It is a true or false value. For example, IsGameOver is a variable we can use on our game manager to keep track of the game over state.
Int
Int or integer is a number variable, which only uses whole numbers. For example, ammo is a value we would assign to an int variable.
Float
A float is a number variable too. This value can contain a decimal point so it does not have to be a whole number. For example, we would generally assign our speed variable to a float.
Other variable types
While the above variables are the most common types of variables we use they are not the only variables we have access to. Consider this list below and take note of the limitations each variable has. Note this is not an exhaustive list there are more rarely used variables we can use in Unity and C#.
Variable names
Generally, we use what is called camel casing to name our variables. This means when naming the variable, the first word starts with a lower case letter and then every other word in the name will start with a capital letter. “myFirstVariable”, “ammoCount” and “health” are all examples of using this naming convention.
When we assign a variable as a private variable a good standard is to start the variable name with a “_” character, so this variable can be easily determined as private when reading your code. For example “_speed”.
Just remember to keep your names unique and as easily identifiable as possible. Using “playerName” instead of “name” could save you a lot of time searching through code when debugging.
That's all for now.