Now the things start to be very exciting!
The first concept when we talk about programming are the variables.
But, what are variables?
We need to think at variables like a bucket which contains our datas.
And we call it “variables” because can change!

Now, in this project we have the “speed” variable, and what we want is not hard coding in number the speed, but use a variable! This because for us have sense can be change the speed at any moment, right?
If we have speed = 5, and we want change to 7, or 8, have sense coming back and update every time?
Absolutely no, that’s why we use variables!

Variables allow us to change, the data in a current time.
In this case, the speed.

First of all, variables have to be declare.
When we declare it, we need to understand a couple of concept.

  1. Variable will have PUBLIC or PRIVATE reference.
    PUBLIC variables can be read OUTSIDE our Player script.
    So every gameObjects or other scripts will KNOWS that this variable exist.
    The PRIVATE variables, on the other hand, will be known ONLY by the Player.
  2. Every variables has a DATA TYPE
    There is four common data type : int, float, bool, string.
    int = integer numbers value,
    float = decimal numbers value,
    bool = “true or false” value
    string = character or text value
    In our case, we need int value or float value, because speed is a number
  3. Every variables has a NAME
  4. This is optional, but every variables can have a VALUE ASSIGNED

And this is the variable declare

Now, if I write like this, I DECLARE the variable, there’s no value assign to it, and to default, Unity set it to zero.

But if I write this

I assign to the variable the value of 5.5.
We need to use f at the end when we use a float value.
If we forgot the f, Unity won’t compile and give us an error.

We can use the integer value, and the result is this

Now we have a variable, but how can we use it?
Well, we simply replace the 5 here

with our variable!

Now, if we back to Unity, and go to the Player inspector, we see something really cool!

We can see the variable, bacuse we declere it PUBLIC.
Every PUBLIC variables is visible in the inspector.

Last thing: we can change the value directly in the inspector, just left click on the little double arrow and move left or right.

Or simply change by double click on value, and rewrite.

IMPORTANT NOTE : the value assigned in the inspector OVERWRITE the value in the script, so if we have speed = 5 in the script and speed = 10 in the inspector, the Player’s speed when press Play is 10.

In the next article we go deep in this topic!

Always in love with programming, even if late (I'm already 39 years old) I decided to follow my dream! We will see how far my passion will take me!