Unity/Programming Guide

Variables, the building blocks of programming

A guide about one of the most important programming elements

One of the most important elements in programming is the variable, because it provides a space to store data and as its name suggests it can vary according to the type of information it stores.

As I’ve been working with Unity and C#, we can define the next properties about the variables:


A variable is normally conformed by the next structure in Unity:

Access reference

This element will define the access properties that a variable contains in the project. The most common are:

  • Private

When this type of reference is selected for the variable, it allows access and modification only to the class where it was created. Also, if no other access reference is selected for a new variable it makes it private by default.

  • Public

When this type of reference is selected for the variable, it allows access and modification to any other classes in the project.

It’s considered best practice to keep the variables private to avoid the manipulation of properties from every other element outside the class.

Data types

These are the elements that will define the type of information that the variable stores. There are 4 common data types in C#:

  • int

Stores integer values, which include the whole numbers but also negative numbers. It can’t store numbers that represent a decimal or a fraction.

  • float

Stores decimal or fraction values. Numbers stored in this type of data should include an f at the end to be recognized as floats.

  • bool

Stores logical values. As it’s a binary variable it can store a false or true value.

  • string

Stores strings of characters of text values. You can store a name, phrase or sentence in this data type.


Every variable needs a name so that it can be called to return the value that is stored in it.

It’s important to give appropiate names to the variables to avoid bad practices when working in a project with thousands of variables. For example: in C#, when a private variable is created, the common thing to add before the name is a “_”, so that when a dev searches for private variables only the ones with underscore appear.

name examples for int values


Finally, an initial value can be assigned to the variables, so that when they’re called in the code they return it. If the variable is created without a value assigned then it’s going to store the default value for the type of data stored.

if I don’t assign true as an initial value for _sexy it’s false by default, otherwise it’s true

Unity Editor

Another aspect to notice is that when we work with Unity Editor the public variables will be visible and editable in the inspector, while the private variables won’t.

Public variable in the inspector
Private variable in the inspector

Unless we use [SerializeField] above the private variable so that we can see and modify the private variable in the inspector:

And that’s it, you can handle variables in C# with Unity! :D. I’ll see you in the next post, where I’ll be showing why do we need to write pseudocode before start programming.

If you want to know more about me, feel free to connect with me on LinkedIn or visit my website :D

A passionate computer technology engineer and Unity developer that is always looking to grow in every aspect of life :).