C# : variables! Useful info, little tips and advice

Matteo Lo Piccolo
4 min readApr 23, 2021


Variables are crucial for writing code. There are some standard things that are best to know and use right away: Unity and C # are “CASE SENSITIVE”. So any lowercase or uppercase matters. As a practice, variables are ALWAYS written in lowercase. For example, the speed variable will be written like this


Variables are often made up of several words, and the way to write them is this: if for example, we have to set the speed of the Player, the variable will be


First word ALWAYS lowercase, but the first letter of the second word will be CAPITAL.
If it were three words, the third word would have a capital first, and so on


Another point: PRIVATE VARIABLES need an underscore in front


same, but with underscore

Sometimes we can see variables written like this

I create a variable called number and assign value of 5

It means it’s PRIVATE.

private int number = 5;
int number = 5;


One of the more important thing is something called SCOPE.
What is the SCOPE of variables?
This is one of the crucial thing about variables.
Here we have a simple C# script.

If we create a TOP variable of this class, the SCOPE of the variable is in the whole Class.
This means that in every part of this class we can ACCESS the variable.
Example : I create a int number, then I try to access to it from Start and Update

I can access to number varaible in any point. If we create a new method, we can access without problems to it

But if I have a method, and inside I create a variable

If I try to access a or b OUTSIDE THIS METHOD, I can’t.
Because the PURPOSE of these two variables is WITHIN THIS METHOD. Out of here, they don’t exist!

In fact, if we try

But inside Sum() i can access to number

In a simpler way, let’s think this way: the curly brackets represent the SCOPE OF VARIABLES.
In our case, “number” is between the two braces that enclose the whole class,
“a” and “b” instead are between the two braces that enclose ONLY THE SUM () METHOD.

Go deep in this topic!
First, we can set the variables value directly in the inspector.
If we have different value in the script and write new one in the inspector, we can reset value simply click this little icon and reset.

The value from inspector is reset at the script value.

Now try to change PUBLIC to PRIVATE and see what happen.


When back in Unity, the variable is not visible!

Everything works fine, the speed value is set to the script, but we can’t adjust that in the inspector.
This because we set variable to PRIVATE and only “Player” can access to it, nothing outside.

The logic is really simple, if we need something outside the Player that has access to that variable, we set it to public, otherwise, is always better to private.

The last tip introduces a concept : “attribute”.
The attribute is something you can write above the variable in square brackets and give a sort of “customization” at that variable.

For now, i’m not go to deep in this topic, we will talk more when use it for “Unity editor” or some particular customization, but is good to know that if we want create a private variable and we want see in the inspector, the method and the syntax is this

[SerializeField] attribute

With this, Unity SERIALIZE the data and we can see in the inspector. So, even if the variables are private, with this we can see and overwrite in the inspector the value of them.



Matteo Lo Piccolo

Always in love with programming, even if late (I'm already 39 years old) I decided to follow my dream! We will see how far my passion will take me!