Variables and its usage

What is a Variable?

Imagine a container that has information in it, this container is what we call a “Variable” and the information it contains is what we call “data”. The type of data the container contains depends upon the “data type”. For example, let’s say that you’re playing an RPG game and as you progress you collect some experience points, this experience point is a variable in this case.

Variable Declaration

There are three required components in-order to create a variable, they are “Access Modifier”, “Data Type” and a “Variable Name”.

Variable Name, is self explanatory it’s the name of your variable.

access modifier, this determines who and what has access to your variables. It’s mainly of three types, private, protected and public.
this ensures that your variable is not accessible by any other class/ method other than its own. Not even the unity editor.
protected, this ensures that your variable is accessible only to the class they belong and it’s subclasses.
public, this makes your variable accessible to everything in the project even the editor.

Data Type, this determines the type and size of data your variable contains. Some of the common data types are, int, float, string, bool.
this deals with integers only. Example, 2, -2, 0
float, this deals with floating point values(numbers with decimals). Example, -2.5f, 2.5f, 0.0f. Note:- when working with float always make sure your variable value ends with an “f”.
string, this data type stores letters, words, sentences, numbers and characters. Example, “”. Note:- when working with strings make sure to use “” double quotes.
bool, this returns a Boolean that is either “true” or “false.

There are other data types too like Vector3 which stores a 3D Vector data.

Let’s say you have a private variable called speed and you don’t want any other classes to have access to it, but you want to be able to change it’s value at run time in the editor to decide on a default value this can be done using,


This was a quick overview on the usage of variables.

Aspiring Game Developer with experience in the field of Machine Learning.

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