Variables are a part of well… a variety of things. In code the variable can refer to something like a character’s name, a weapon, a powerup, or a score, etc.
Anything that can hold information can be a variable.
When working on a script an important component to variables can determine who has access to the code. Setting a public variable allows anyone to see it, and if connected to Unity that variable could be accessed directly from the Unity program. Private, on the other hand, allows only the script itself to touch the variable.
How do you tell which one to use?
Well if the information needs to be seen by the player or anybody else, the variable should be set as public. This could include things like health, score, magic points, inventory, etc.
But any variable that runs in the background and is not seen, sometimes this could be the damage an enemy makes, can be set to private. Ultimately, this is a case-by-case basis. Always think about what you want everyone to have access to.
The next component involves data types.
Let’s break down some of the more common data types:
Int: This used for integers. Any variable that has a whole number value between -2,147,483,648 to 2,147,483,647 can use the int data type.
Float: Like int, this is used for numbers; however, float can store decimal values up to 6 or 7 decimal places. (Also remember to type an “f” at the end of the number you’re using.)
Boolean: This is pre-determined because a boolean can only be one of two things: true or false. In gaming, the variable will likely be a yes or no question that checks to see if a condition has been met. This data type will likely have a lot of use in “if-then” scenarios that we will get to in a later article.
String: This is used for variables that contain more than one character (as in type, not games). This could be used for creating messages, making names show up, etc. Text does need to be in quotations.
Also, never forget, every variable needs a name.
As you might have noticed, there is a common way of making names by cameling. This means that if the name contains more than one word, the first word is not capitalized, while the words following are capped. This makes it easy to read and efficient when checking code.
Variables do not have to be constant. Once a variable has been assigned, the value can later be reassigned through the script’s code, or through Unity when the variable has been made public. Let’s say the main character of the game you’re making is officially named Wade, but you’d like the player to be able to assign their own name.
Here, the variable “mainCharacter” is assigned the value of “Wade.” As its public, through a series of coding steps we’ll get into in a further article, the player can reassign the variable. So mainCharacter would equal whatever name the player picked out, and that value is what will be used unless the variable gets reassigned again.
This is the basics of variables, they will be a constant presence in any sort of coding. There are many more types to fill specific niches and requirements, but they should follow the same basic principles. Have fun coding!