Summary
A variable is a named container that holds a value the game needs to remember. C# is a statically typed language — every variable must declare its type, and that type cannot change. The four types used throughout Unity game scripting are int, float, bool, and string. Choosing the right type matters: speed can be 2.5 units, but not 2.5 lives.
Key ideas
Core types:
| Type | Stores | Game examples |
|---|---|---|
int | Whole numbers | score, lives, level, health |
float | Decimal numbers | moveSpeed, jumpForce, timer |
bool | true or false | isAlive, isGameOver, isGrounded |
string | Text | playerName, levelName |
const: A value that is fixed at compile time and can never be modified. Use it for values that must not change, such as maximum health or a fixed damage multiplier.
Access modifiers in Unity:
public— accessible from other scripts; also appears in the Inspector.private— accessible only within this script; hidden from the Inspector by default.[SerializeField]— makes aprivatefield visible in the Inspector without making it accessible to other scripts. Preferred overpublicwhen Inspector access is needed but external access is not.
[Header("...")]: An Inspector attribute that adds a bold label above a group of fields. Used to organise related variables visually in the Inspector.
Arrays: An ordered collection of values of the same type. Size is fixed at initialisation. Elements are accessed by their zero-based index; the Length property returns the number of elements.
In practice
// Basic type declarations
int score = 0;
int lives = 3;
float moveSpeed = 5.5f; // float literals require the 'f' suffix
bool isAlive = true;
string playerName = "Hero";
// const — value cannot be changed anywhere
private const int MaxHealth = 100;// Inspector-visible fields using public and [SerializeField]
[Header("Player State")]
public int health = 100; // public — visible in Inspector, accessible to other scripts
[SerializeField]
private float jumpForce = 8f; // private — visible in Inspector, not accessible externally// Arrays
int[] coinValues = { 10, 25, 10, 50, 15 }; // initialise with values
string[] levelNames = new string[4]; // initialise empty with size
// Access by index (zero-based)
int first = coinValues[0]; // 10
int last = coinValues[4]; // 15
// Length property
int count = coinValues.Length; // 5Compound assignment operators:
score += 10; // same as: score = score + 10
score -= 5; // same as: score = score - 5
health *= 2; // same as: health = health * 2Gotchas
floatliterals must have anfsuffix:5.5fnot5.5. Without it, C# treats the value as adoubleand the assignment will fail to compile.- Array indices start at zero. An array of 5 elements has indices 0–4; accessing index 5 throws an
IndexOutOfRangeException. - Prefer
[SerializeField] privateoverpublicwhen you only need Inspector access. Making a fieldpublicfor Inspector convenience exposes it to unintended external mutation. constfields are implicitlystatic— they belong to the type, not to an instance. They cannot be modified at runtime and cannot be exposed to the Inspector.
Related
- csharp-control-flow — using variables in conditions and loops
- csharp-methods — passing variables as parameters
- monobehaviour-lifecycle — where variable declarations live in a Unity script
- unity-inspector-references — assigning object references in the Inspector