01 / 11 · 4 min
Basics
C# in 60 seconds: variables
your project so far
Nothing to open yet. Unity first opens in lesson 05 — these first four lessons are pure C#.
A variable is a named box that holds one value. That is the whole idea.
animated diagram
Declare the box (type + name), then values go in and out of it while the game runs. The fourth box holds three numbers at once, and together they are a place on the floor.
The four types you actually need
the whole of C# basics, in 6 lines
int hp = 100; // whole number
float speed = 5.5f;
bool isDead = false; // true / false
string name = "Knight";
Vector3 pos = new Vector3(0f, 0f, 0f); // a point in 3DThe type on the left tells C# how big the box is and what fits in it.
float always needs the f suffix — 5.5 alone is a double and will not compile.
Vector3 holds three floats at once: x, y, z. In Unity you use it constantly.
Naming: use lowerCamelCase for variables — that is the Unity convention.
Changing a value
C# script
hp = hp - 20; // long form
hp -= 20;
hp += 5; // heal
speed *= 2f;Do this now
Nothing to open yet — just read the 6 lines above twice. The next lesson puts them inside a real Unity script.