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.
  2. Data Type: This is the kind of data that can be stored in the variable. There are many different data types but the most commonly used are; int (integer), float (floating-point number), bool (boolean), and string. An int is a whole number, a float is a number that can have a decimal, a bool is a true-false statement, and a string is a string of letters or characters. The data type can be more complex and customized to hold any combination of data.
  3. Name: What the programmer labels the variable so it can be identified and used in code.
  4. Value: Is the actual data recorded in the variable and is optional because it defaults to null (empty or nothing) in the case of most data types, 0 in the case of numbers, or false in the case of a bool.

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.



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
Kyle W. Powers

Kyle W. Powers

Unity Developer, Software Engineer, Game Developer