Play with Variables- Game Dev Series 04
You need to tell Unity what it is!
Previous: Player Movement
A variable is like a storage box. You can name the box, definite the type, give the data, and take out whenever you want.
Variables can be used in many ways depend on the type.
There are 4 common variables in Unity:
string
: for containing text or sentence.int
: for integerfloat
: for float numbersbool
: for boolean
To declare a variable in Unity, we need to make sure whether is public
or not, then given a name.
A public
variable is public to everything, unlike private
can only be used in its own script. You need to add public
or private
at the beginning of a variable, or it will be private by default.
A public
variable can also let you easily to adjust in the Inspector when attached to a game object.
To naming a variable, always follow the rule called camelCase.
A camelCase is to set the first letter of each word in a compound word is capitalized, except for the first word.
You can use variables to do some calculation.
If you want to combine int
and float
, make sure declare a float
as result to prevent occur an error.
You can also make complex calculation in one line.