In this tutorial we will look at how to store data in a game using variables and also the different data types that are used in the C# language – the language we are using for scripting in Unity.
C# Data Types
A data type classifies various types of data eg. string, integer, float, boolean, the types of accepted values for that data type, operations that can be performed on the data type, the meaning of the data, and the way that data of that type can be stored.
The table below shows the data types available in the C# language.
||The integer data type stores positive or negative whole numbers eg. 5
||The float data type stores floating point numbers (numbers with decimal places). The float data type is the default number type in Unity eg. 5.25
||The double data type stores floating point numbers but can hold larger size numbers than floats. It is not the default number type in Unity though.
||The bool (short for Boolean) data type stores true or false values only eg. true
||The char data type stores a single character such as a letter, number, space or special character (eg. a, 1, !). A char value is written in single quotes eg. ‘a’.
||The string data type stores letters, numbers and other characters in the form of words or sentences. A string value is written inside double quotes eg. “Hello World”.
A variable is a location that can store temporary data in a program. It is like a ‘container’ that can store a value that can be used within the program. To create a variable it must firstly be given a name and a type. Variable declaration in C# uses the following syntax:
<variable type> <variable name>;
Variables should be given meaningful names. Variable names should never contain spaces. To declare a variable that is of the integer data type and is called myNumber, you would write the following:
You can also assign a value to a variable during its declaration like this.
Or you can assign a value on another line after the variable has been declared like this:
To declare a string variable, you would write something like this:
And you can also assign the value during declaration like this:
string message = "Hello World";
These are just a few examples of how to create variables in C#. Remember that strings are contained inside double quotes and chars are contained inside single quotes. Integers, floats, doubles, and boolean values are not placed inside quotes.
The scope of a variable dictates where it can be used. As mentioned earlier, classes and methods use opening and closing brackets and everything inside those brackets is called a ‘block’.
Variables can only be used within the block that they were created in. If a variable is created inside a class, it can only be used inside that class. If a variable is created inside a method (or function), it can only be used inside that method. For example, if you create a variable inside the Start method, it can’t be used inside the Update method because they are two different blocks.
However, if a variable is created in a class but outside of all of the methods on that class, it will be available in all of the methods in that class because those methods are in the same ‘block’ that the variable is in.
Public and private
In code, you might see the keywords public or private. These are known as access modifiers. Private variables can only be used inside the file that they were created in. This means that other scripts or code editors cannot see or modify these variables.
However, public variables are visible to other scripts and can even be seen and modified inside the Unity editor (Inspector). This makes it really easy to change values for variables right in the Unity editor without having to change the values in your code editor such as MonoDevelop.
To make a public or private variable, just add the private or public keyword before the variable type and name during declaration, for example: