Variables in C#. What are they, what do they mean?!

If you’ve started undertaking C# programming, within Unity or otherwise, you’ve no doubt come across variables. They are quite literally the building blocks of better code. They help keep things organized and flexible, and if you aren’t using them, you SHOULD BE. No one wants to chase down a “magic number”.

So what are variables really? They come in different shapes and sizes, but ultimately, they are a word reference to a value of some sort. They are structured in such a way: <reference type> <data type> <name> = <value>(optional)

Let me explain…

Let’s start with the <reference type>: here we have two main types that are being used — public and private.

  • Public variables can be seen in the Unity editor and manipulated by other scripts through code. These are useful for variables you know will be interacting with other scripts.
  • Private variables can only be seen and manipulated in the current script. These are internal variables that you want to keep untouched by other scripts.
  • [SerializeField] allows private variables to be seen and manipulated in the Unity editor, but still not accessible through other script’s code. Useful if you have separate designers on your team to tweak attributes, but don’t want them messing with code.

Here’s a look at the Unity editor of a GameObject with this script attached:

Notice that attackRate is not visible because it is private, but isGameOver is visible because it has [SerializeField].

It is also important to note that values can be changed in the Unity Editor. These changes will overwrite the script code.

Next we have the <data type>: in the same example we have four different data types — float, int, string, and bool.

  • Float variables are 32 bit floating point types. They use decimals, and can range from -3.402823e38 to 3.402823e38. Use floats if you want to use a value other than whole numbers. **Important** — float variables must have the suffix “f” at the end of their value, to denote it being a float: 3.5f, for example.
  • Int is short for integer. It is also 32-bit and can range from -2,147,483,648
    to 2,147,483,647. Use integers if you’ll by using values of whole numbers. Good for a basic health system, or counting inventory slots. They CANNOT use decimals.
  • String variables are a sequence (string) of characters, that must be encased by quotations. “frankguitar11” for example. Be cautious when spelling strings — whatever is inside the quotations, spelling errors and all, will be accounted for.
  • Bool variables are much simpler and can be either true or false. isGameOver is a perfect example because the game is over or it’s not. This variable is essential when writing game loops and logic. In our example above, isGameOver is not assigned a value. Bool variables default to false, unless otherwise stated.

The <name> parameter can be anything you choose. It is best to keep it relevant, for the sake of organization, and not racking your brain in a week wondering why you called a certain int Ireland?

The <value> is optional. You can set it when creating the variable, when you Start() the script, through Update() or other methods/functions. You can also leave it blank, where the default state is 0 for ints/floats, “blank” for strings, and false for bools.

This is just a quick overview of some common variables, but every variable must follow the structure of <reference type> <data type> <name> = <value>(optional).

You can check out this link: for more information on different kinds of data types!

Good luck, happy coding!



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
Frank Warman

Frank Warman

Audio Engineer turned Unity Game Dev. Will be combining both my skillsets when appropriate, and will be documenting my Unity Dev growth in these Medium Articles