Variables(Unity and C#)

Chris Daley(Desonn)
2 min readApr 3, 2021

Variables are data containers, which allow us to hold or change data, reference. These variables can be of different types, all referenced with different names, so we can clearly understand what kind of variable are them and what functionality we want to give them.

There are several types of variables the most common are:

  1. int — a whole number that can be positive or negative.
  2. float — a number with decimal places.
  3. bool — a true or false condition.
  4. string -can be letters, numbers or other characters in the form of words or sentences. It is written with double quotes.

Variable can be private or public and sometimes protected. We will focus on private and public for now

Public variables can be shared and accessed from other scripts.

Private variables can only be used in the script they reside. A naming convention usually starts with a lower case letter to identity the difference

Follow this syntax when writing variables

<private or public><variable type><variable name>

private int speedBall;

Best Practices when using variables

  1. Always try to use descriptive names.
  2. Always start with a lower case letter in the name of a private variable.

One last tip on variables, if you want to be able to change the value in the inspector while testing your game, you and serialized filed. See example below and the outcome which will allow you to work more easier in unity.

--

--