C# for Unity — Lesson #1: Data Types and Variables
February 25 2021
What are Data Types
Computers work by manipulating pieces of data, like adding numbers or concatenating strings, for example. A data type defines the operations that can be done on the data, the meaning of the data, and how much space this piece of data needs to be stored on the memory. Without them, programs could end up trying to multiply a text, for example.
Data types can also help the programmer write the code. When all data types are explicitly defined in a script, understanding the code becomes much easier. It also works as built-in documentation. In C#, you are required to specify the type of everything, but that is not the case for every programming language.
Common Data Types
There are well over 10 data types in C#; luckily, we can get away with way less than that for game development in Unity. The built-in data types that you must know are:
When declaring texts, always remember to put them inside “double quotes”.
When declaring floats, always remember to add an ‘f’ at the end of the decimal number: 9.99f
Data types are used all across your code, and we will go over each of these usages, starting with variables!
During the execution of your code, we need a way to store data for future usage. It would be foolish to calculate a super complex formula just to forget it a moment later. Variables exist for this. Think of them as little boxes where data is kept for later usage.
But then, how big should that box be? Or in other words, what are we storing? That sounds like a problem for our buddies Data types!
Variables and Data Types are deeply intertwined; To declare a variable, you must always give it a name and a Data Type.
With a variable declared, we can use them throughout the program, much as we did on that last statement.
The actions performed by a program are expressed in statements, declaring a variable is an example of a statement. Every statement should end with a semicolon ( ; ).
The variable names must follow some rules, keep them to common Latin letters, and you will be fine.
The two forward slashes ( // ) in the snippets above are used to add a single-line comment to your code. Anything between the slashes and the end of the line will be completely ignored when executing the program. This is a great way to explain your code and make it more readable.
Converting Data Types
Often you will have to use a variable as it were from a different data type, that is called converting. Some examples are:
- Display a number in the UI (Text).
- Distribute a number of enemies (int) in space (float).
- Trying to read a value from a user input string.
C# will convert values automatically on some usages, referred to as "Implicit Conversion," but that only happens if no loss of data would incur. In our second conversion, we would lose the decimal values stored in a float, and so, the C# compiler raises the error:
“Cannot implicitly convert type 'float' to 'int'. An explicit conversion exists (are you missing a cast?)”
So, as the message tells us, there is an explicit conversion:
“(int)” is called a “cast operator” and is used to explicitly tell the compiler to convert that value to the casted data type.
Operators manipulate numeric variables and values to create new ones, just like common math would.
Order of Operations
C# calculates the final value of the expressions using the order of operations, similar to how we do in common Math. So, multiplication and division are resolved before addition and subtraction.
Data Type Conversion
Now, are the results from these two lines equal?
The answer is NO!
In the first line we are dividing integer numbers, so the result is rounded to the nearest integer, making the assigned value = 1.
On the second line, since we have number 4 declared as a floating point number (4.0f), the operation is completed using float as the target data type, making the assigned value = 1.25f;
On the third line, even though we have the number 1 declared as a float, due to the order of Operations, first the program will resolve the division, which is between two integers, this leads to a loss of fraction just like the first line, making the final assigned value of the whole expression = 1f.
When using operators, it’s essential to pay attention to data types. C# combines the operations always using the data type with the most information.
Be careful with your data types 🙂
Complex mathematical operations in Unity are done using a built-in class called Mathf.
In future posts we will go through what is a class and how to use them, but for now a small snippet of how it looks like:
Coming up next!
Next week we’ll take a look at Programming Logic and Conditionals. Now that we've got our bearings with what data types are, it’s time to dive into how the code statements are executed and how we can influence that!
Happy coding 🙂