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.

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.




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

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

What is Programming?

Part 7: Emma’s Composable Commerce Architecture (3 of 3): The MAKE Parts

Getting the index number of an item in an Array of Identifiable

MongoDB Replication: A complete installation guide(with scripts)

The Most Prominent DevOps Tools Expected To Dominate In 2022

Libraries and differences between them

Unit Testing

Chihiro BIOS Download

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
S.J. Jason Liu

S.J. Jason Liu

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

More from Medium

High Definition Render Pipeline in Unity- Game Dev Series 158

Unity: Wrapping Player Movement

Contemperary dancer leaping in a small room with white walls and black floor

Loading Scenes in Unity

How to Instantiate and Destroy Game Objects in Unity