Variables: Programming Building Blocks!
What are code variables?
As the name suggests, variables are containers for of data which can change. Variables can be of many different types, and each type is designed to hold a specific type of data.
There are 2 required parts to writing a variable in C#. These are the Data Type, and the Variable Name.
There are also 2 additional and optional parts, which may be used in certain situations. These are the Access Modifier and the Default Value.
Why should you use them?
As said above, Variables are how data is stored. So, if you want to store any data, you’ll need variables to do it, whether they’re defined in the class like these examples, or in a more temporary way. Variables are essential for anything more than the most basic of code. Plus they have the added benefit of giving nice easy-to-understand names to your data, so instead of trying to remember what “3.14f” means and does, you can know that “_animationDuration = 3.14f” is the duration of your animation.
What do the different parts mean?
Access Modifiers (Optional):
These define how and where the variable can be accessed. There are 3 common access modifiers used in conjunction with Unity. An Access Modifier may not be used if the variable is defined within a function, meaning that it only exists while that function is running. As such, it doesn’t need a modifier because it’s private by force.
The “public” modifier means that the variable can be accessed by any script within the project, provided it is within the same Assembly Definition (Assembly Definitions are a whole other complicated topic, and are rarely used in most projects).
The “private” modifier means that the variable can only be accessed from the script it is defined in, and not from any other script or part of the project.
The “protected” modifier is somewhere in the middle of the above 2. It means that the variable can be accessed only from the script it is defined in, as well as any script that inherits from the script it was defined in.
Data Types are the type of data the variable should, and can, store. Some common data types often used in Unity are:
Int (short for Integer): A data type for storing whole numbers, e.g. 1, or 18, or 38562924.
Float: A data type for storing decimal numbers, e.g. 3.14, or 46.1, or 185.2856. Values of type Float must always be followed by the lowercase letter “f”, to differentiate them from Integers.
String: A data type for storing a series of alphanumeric characters, e.g. “Vincent”, or “A Random Sentence!”, or “jfhSkuVKmeE”. Values of the type String must be surrounded by quote marks.
Bool (Short for Boolean): A data type for storing true or false values, which can only ever be either true or false.
The variable names are simply the name used to identify that variable when trying to reference or access it from code. There are many industry standards for ensuring easily understandable names and conventions, but that’s for a different conversation.
Variable names cannot contain spaces.
Generally in Unity and C#, it is common to use a standard called Camel Case, which says all names should start with a lower case letter. If the name contains multiple words, any words after the first should start with a capital letter. Any private variables should also have an underscore (“_”) at the beginning of the name.
Default Values (Optional):
Default values can optionally be appended to variable definitions in some cases. This is done because, for one reason or another, the programmer needs the variable to be initialized with a value different from the default value set by its data type.
Unity Inspector Example:
If we write a class like this:
It appears in the Unity Inspector like this:
The SerializeField Attribute:
You can see that the above “_exampleString” variable has “[SerializeField]” written in front of it. This is something called an Attribute, and changes how a variable works, or is displayed.
The SerializeField Attribute makes the private variable, which would normally be hidden in the Inspector, appear in the Inspector like a public variable. It can be modified in the Inspector and the modified value is kept, but the variable is still inaccessible from code from outside of the ExampleScript script.
It is used to allow a variable to be modified in the Inspector, while also keeping it safe from unintentional or badly-designed code modifications.