An Artist's Guide to C# in Unity3D: Common Data Types and Variables
This article is part of an ongoing series for code-leaning artists who want to learn Unity 3D. In this article I introduce you to some common data types in C# and how to use variables.
This article is part of an ongoing series for code-leaning artists who want to learn Unity 3D. In this article I introduce you to some common data types in C# and how to use variables.
Read previous: Creating your first script
Common Data Types and Variables in C#
In any program you’re going to be dealing with data values of different types. You may want to store a player’s age, name or calculate a character's speed, health points, etc. These are all different types of data you may need to store, manipulate or calculate.
C-sharp comes with several built-in data types it can handle, but you don’t need to learn them all. For eighty-percent of your game development needs, you’ll use the following five built-in data types.
Common Data Types
Variables
If built-in data types are the values you use in your program, then variables can be thought of as named slots or containers for holding those data values.
Variable declarations take the form of:
<data type> <name of the variable>;
Or, if you want to assign a value to the variable in one go (called initializing the variable):
<data type> <name of the variable> = <the value to assign to the variable>
In lay terms, what you’re doing with this one-liner is:
Declaring what type of data container you want
Specifying the label/name you’ll use to identify this container
Placing the value you want into the container
For instance, let's say you need to print out a player’s full name in twenty different places, you could do something like this each time:
Debug.Log(“Michelangelo di Lodovico Buonarroti Simoni”);
However, you would have to type that long name twenty times in all the locations you need it. A better alternative would be to assign the player’s name to a variable called fullName
. Now you can use fullName
anywhere you need to print the name. So your code would look something like this:
string fullName = “Michelangelo di Lodovico Buonarroti Simoni”;
Debug.Log(fullName); //prints the full name
You created a container called fullName
to hold the value “Michelangelo di Lodovico Buonarroti Simoni.”
This container can be passed around your program as needed.
Later in your program, you can still change the value of fullName
to anything you want as long as its a string:
fullName = “Leonardo Da Vinci”;
Debug.Log(fullName); // will print out “Leonardo Da Vinci”
If you tried to do this, you would get an error:
fullName = 34; // Error!
Since the variable, fullName
, was declared as a string type, attempting to assign non-string values to it will generate an error.
For now, here is how to declare variables of the five data types mentioned above.
Integers
In C#, integer variables are declared like this:
int coins = 10;
They are good for representing any number values that don’t require a fractional amount.
Floats
In C#, float variables are declared like this:
float gravity = 9.81f;
C# requires that the ‘f’
be added at the and of any float value literals so the value may be correctly recognized as a float.
Booleans
Booleans are simple true or false values and cannot be anything else. They are declared like this:
bool isAlive = true;
Characters
Characters and single character values stored between single quotes. They are declared like this:
char group = ‘D’;
Strings
Any length of characters strung together between double quotation marks are stored as strings. They are declare like this:
string motto = “Give me freedom or give me death!”;
Try It Out
The best way to learn these is to try them out for yourself. So here’s your assignment.
Open up your Fundamentals.cs
script and try creating the following variables in file. If you’re wondering where to place the variables, my recommendation is to put them above the Start()
method (inside the Fundamentals
class block). Do not forget to save your work.
Add a variable to store the nickname of a player. Call the variable
nickName
and initialize it to anything of your choosing.Add a variable called
health
to store the player’s health. Let’s assume a player's health is measured as a whole number between 0 and 5.Add a variable to store the player’s speed. Speed values will have decimal parts.
Add a variable to store whether a player is invincible or not.
For each new variable you add, print it out in the
Start()
method
When you’re finished, your script should look similar to this
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Fundamentals : MonoBehaviour
{
string nickName = “Picassolo”;
int health = 5;
float speed = 3.6;
bool isInvincible = false;
// Start is called before the first frame update
void Start()
{
Debug.Log("Unity Rocks!");
Debug.Log(nickName);
Debug.Log(health);
Debug.Log(speed);
Debug.Log(isInvincible);
}
// Update is called once per frame
void Update()
{
}
}
Next, we’ll learn about what sort of operations can be performed on these data types.
An Artist's Guide to C# in Unity3D