There are many different types of variables in C#, but all variables consist of four parts, with three of those parts that have to be defined and one that is optional to define.

Part of a Variable

The four parts are:

  1. Accessibility: How accessible the variable is to other scripts. The main two types of accessibility are private, which means the variable can only be accessed in the script it was created in (a variable is private by default unless made another accessibility), and public which means any script can access it. There is also protected, which means the variable can only be accessed in the script it was created in and scripts that inherit from that script.

Now we know what a variable is, let’s talk about naming conventions.

The first thing is using camel-case when naming a variable or anything really in code. Camel-case is when the first letter of the first word is lowercase, but then each word after that has an uppercase for the first letter without any spaces.

Example of Camel-Case

The nice thing about using camel-case is that Unity will recognize them as different words when it puts them in the Inspector.

Variable in the Inspector

The second thing is when naming a private variable; it is common practice to put an underscore in front of the name so that way it is easy to tell that it is a private variable anywhere in the script.

Private Variable Example

A private variable will not show in the Inspector unless you serialize it.

Serializing a Private Variable

A protected variable will also not show in the Inspector unless serialized.

With that, variables have been defined, and how to name them has been covered, so now you can go forth and make variables in a standard manner so other programmers can read your code with ease.

Unity Developer, Software Engineer, Game Designer