C# Variables with Unity

Unity works with C#. And one of the first elements we need to understand are variables. A variable has three required and one option element:

  • a scope (public, private, local)
  • a data type (int, float, bool, string)
  • a name
  • a value (optional)

The syntax is: <scope> <data type> <name> = <value>
Example:

All variables can either be public, private or local. We need to declare the public and private reference. The local scope is implicit.

Public
A public variable can be accessed anywhere, outside of the specific class or script. We only set a variable public if we have a need to change them elsewhere. There is a risk somebody changes the variable elsewhere and this leads to problems.

Private
A private variable is not accessible outside of its class. It is the most commonly used option.

Local
A local variable is only valid in the exact section is is defined, i.e.inside its current curly brackets { }.It does not need a declaration as local.

There are four explicit data types used:

int — a whole number, either positive or negative, example
float — a number with decimal places
bool — boolean, means it is either true or false
string — a text with letters, numbers or other characters written in double quotes

Examples:

C# does have the option for implicit data types. The declaration is then done with var and the compiler will attempt to identify the data type. This is however not recommended as it is slower, can lead to errors and is not allowed in mobile development with Unity.

Make sure to give your variable a descriptive, meaningful name, so that it is easy to understand what your variable does. Make sure it is a unique name.

Allowed are letters, digits and the underscore (_underscore), but it can only start with a letter or underscore, never with a digit.

Remember that names in programming are case sensitive. Thus “startingSpeed” is not equal to “StartingSpeed”. Which brings us to the case we use.

PascalCase vs. camelCase
By convention PascalCase is used for public variables, while camelCase genearlly with a starting underscore is used for private variables. PascalCase means we write the name without space and capitalize each letters of a new word. In camelCase the first word uses a lower case, while the following word uses an upper case. Local variables generally use camelCase but no underscore.

Example: MySpeed)
Public variables will always be visible and adjustable in your Unity interface to test some values. But if you want a private variable to be visible as well, then you need to add a serialized field.

Now we are ready to give it a go in Unity.

--

--

Passionate Game Developer and Learning Expert. I love to create games and interactive experiences using Unity, Articulate, C#, JavaScript, PHP, HTML, CSS.

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
Andrea Zilio

Passionate Game Developer and Learning Expert. I love to create games and interactive experiences using Unity, Articulate, C#, JavaScript, PHP, HTML, CSS.