Play with Variables- Game Dev Series 04

S.J. Jason Liu
3 min readApr 29, 2021

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.

Photo by Curology on Unsplash

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 integer
  • float: for float numbers
  • bool: for boolean
4 common types of variable

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.

isAwake is 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.

camelCase is easier to read

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.

error for wrong variable
float can fix it

You can also make complex calculation in one line.

--

--

S.J. Jason Liu

A passionate gamer whose goal is to work in video game development.