Variable in Unity!
We learned how to write basic scripts. That is no small feat, you should be proud. Today, we are going to learn how to incorporate variables into your code. ‘What’s a variable,’ you say. Variables are the spice of life, my friend.
Variables, think of them as a box that contains information, and that box of information can change. It’s called a variable because it can vary/change.
To create a variable you need 3 things, with an optional 4th. First component is a public or private reference.
Public reference means anyone in the outside world can communicate with the variable. Other game objects and scripts can know that the variable exist.
Private reference means only the player knows the variable exist.
The second component is the data type. Every variable has a data type, and there are 4 common data types.
Int = whole number
Float = decimal number
Bool = true or false
String = characters of text
The thirst component is a name. Every variable has a name.
The optional forth component is a value assigned.
Let me show you an example to help put this into perspective. If we wanted to declare variable for speed that supports float values, we would need to write this.
The f stands for float, and if you forget the f Unity will throw an error that specifically states you forgot the f. Public, is the reference, float is the data type, and speed is the name.
If you wanted to you could have written, public float speed; and Unity would give this a default value of zero.
To put this variable into action lets reuse our previous code from the last lesson.
We can replace the number five with the name of the variable.
Once you save the new line in your script you can head back to Unity. You will notice the script component in the inspector view now shows the speed. You can now directly change the speed of the player from the inspector. This will override the script code, without altering the script code. You can also alter the speed of the play in real time, while playing the editor you could increase and decrease the speed to your liking, and if you were to put the number in the negatives the object out moves backwards.
If you needed the script to override the inspector all you need to do is reset the script component in the inspector. Select the setting symbol in the top right corner of the script component and select reset. If you used the code, I showed up above the speed would default back to the 3.5
If we made this a private variable instead of public, the speed would not be visible on the inspector view.
It is a good idea to decide early on if the variable is going to be public or private. If it is a private variable name it with an _ attached to the front of the name.
Doing this will make let you know at a glance if the variable is private or public. when you are working on a large project you could have up to thousands of variables to ready through and being able to easily identify public or private will make your life easier.
If you wanted to have a variable be private, and you wanted your developers to be able to adjust the speed and not have to change the script every time you would need to add an attribute. The attribute needs to be placed above the variable.
This will serialize the data so it can be seen in the inspector view and overridden as if it were public. Other game objects and scripts will not have access as it is still private.
There we have it. We have now added variables to our code and can manipulate it to match our desired effects. Thank you for coming along with me on this journey. Next time we are going to talk about pseudo code! Remember to have fun, and do not be afraid to experiment with your code!