The basics of Variables in Unity

Since the Unity engine uses C# for it’s API, for the most part it does all the standard things C# can do. Since C# is part of the Microsoft .NET family, the best place to look for the basic types of variables and their uses is on the Microsoft website.

All the basic value types C# provides, there is a separate list of reference types too

One very clever thing that Unity does for variables is provide the SeralizeField attribute. Unfortunately this feature in particular isn’t especially well documented.

OK, but what does that mean?

Most importantly what SerializeField actually does is expose a variable to the Inspector when you have the object selected in the scene view.

Adding the attribute to a variable like this…
..makes the variable show up like this

The variable will even update as it changes while playing the game through the editor, making it a great way to debug what is happening without resorting to log messages or the profiler. You can also change the variable directly while the game is running, allowing for some very fast iteration if you want to try out some different numbers to get your game balance right. One important thing to note is that any changes made while your game is running won’t be saved, so you have to make sure to make note of any changes you want to keep and re-enter them once you stop the preview.

Game developer with over 12 years of experience, mainly focused on MMOs.