Variables-basic building blocks of programming (C# as an example)

A Variable is like a holder of a value. In the context of game development, a Variable can be the player’s life, moving speed, score, the number of bullets, inventory items, number of items, stamina, mana, etc…Literally any attributes of an object needs to be held in a variable.

The computer does not know what a life, speed, bullet number is, it has to be created.

To construct a Variable in the C# script, there are four components:

  1. A public/private/protected reference, which defines who has access to this variable.
  2. The data type (string, int, float, bool, …)
  3. A name (name the variable to make it descriptive)
  4. Optional, a value

In the context of game development using unity, any component can be a data type:

All those public variables created will show up in the inspector of the Unity editor:

Some best practices when it comes to variables:

  • Syntax difference between public and private variables. Use “_” before the private variable name;
  • All variables should be private unless it needs to be accessed by other scripts;
  • Make the variable name descriptive, so that it can be understood by others and yourself later on.

--

--

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