Asteroids – Variables
Hi guys and welcome to a basics tutorial all about variables.
To begin open up the MyScript script we created last time (if you skipped the last part make a new script called MyScript).
We are going to add four new variables to our class. Each one is a different type of variable. Variables have a type and the four most commonly used ones are string, int, float and bool (see below for explanations). They also have a name which can be whatever you like. It should be noted there are more than four types of variable, however we won’t necessarily cover these in this tutorial series.
Strings are variables which contain letters i.e letters, words, sentences and paragraphs. It should be noted numbers can also be used in strings however they will need to be converted to int or float to be used in calculations.
Ints are whole number variables for example 1, 2 , 100, -10 etc.
Floats are numbers with a decimal point for example -1.5, 3.14159, 1000.0001 etc. It should be noted whole numbers can be floats too. If you declare a variable to be a float and set it equal to 1 it will work. Floats used in calculations require an f be written after them otherwise they will be considered ints or doubles (doubles are just like floats but take up more memory as they can be larger numbers than floats, either in the positive or negative direction.) E.g. 1f, 9.5f, 1000.0001f etc.
Bools are true or false. They are essentially a two state condition for example a coin toss can only be heads or tales.
A Variables type must always be specified (declared) before it can be used. You can declare and set a variable at the same time or you can declare the variable some time before using it.
Generally you declare variables which will be used by multiple functions or other scripts at the top of the class. Variables which are required once in a function are usually declared and set on the same line. If a variable is declared and set in a function it is “garbage collected” after the function is complete meaning it is no longer held in memory. This will make more sense when we start to look at functions/methods in more detail.
After our class starts but before the Start method, add the code in the picture below. We are declaring the variables here so that each method in the class has access to them.
Notice how each variable has its type next to it. Remember all variables need to be declared once before they can be used.
A variable’s name is case sensitive. For example string player is different to string Player and string PLAYER. It is common in C# to use the camel case with variables; this means you start with lowercase but every word after the first starts with a capital letter (like in our variables we just added). You cannot put spaces in variable names. It is however your preference how you want to name your variables. As long as you keep it consistent there is no right or wrong way to do it.
Assigning Values to Variables
Let’s assign some values to these variables. We will do this in the Start method however we could have done it as soon as we declared them. We could also do it in the Update method but this would be very inefficient as it would just constantly be assigning the variable the same value over and over again.
Notice how we don’t have to put the variable type again. That is because we have already declared the variables above.
At the moment these variables don’t really mean much. In the next part I will show you how to make a GameObject in Unity and assign it this script. We will then start to see some context.