📦 Lesson 3.2: Variables & Public Inspector Fields
A variable is a named box that holds a value — a number, some text, a yes/no. And here's Unity's magic touch: make a variable public and it shows up in the Inspector for you to tweak without touching code.
🎯 Learning Objectives
- Understand what a variable is and how to declare one
- Know the four everyday types:
int,float,string,bool - Expose a variable in the Inspector using public or [SerializeField]
- Change a value in the Inspector and see it affect the script
Estimated Time: 35 minutes
In This Lesson
What Is a Variable?
A variable is a labelled container for a value. You declare it by stating its type, its name, and (optionally) a starting value:
int score = 0; // a whole number, starts at 0
float speed = 5.0f; // a decimal number, starts at 5
string playerName = "Ada"; // some text
bool isAlive = true; // true or false
Later you can read it or change it:
score = score + 10; // score is now 10
speed = 8f; // change the speed
isAlive = false; // the player died
📖 Definition
Variable: a named place in memory that stores a value of a specific type. The type decides what kind of value it can hold and what you can do with it.
The Four Everyday Types
| Type | Holds | Example |
|---|---|---|
int | Whole numbers | int lives = 3; |
float | Decimal numbers (note the f) | float speed = 4.5f; |
string | Text (in quotes) | string name = "Hero"; |
bool | true / false | bool hasKey = false; |
⚠️ Thefon floats: C# needs anfafter decimal numbers used for floats —5.0f, not5.0. Forgetting it is the #1 beginner error message in Unity. If you see "cannot convert double to float," add thef.
Public Fields in the Inspector
Here's Unity's superpower. Mark a variable public and it appears in the Inspector as an editable field — no recompiling to try new values:
using UnityEngine;
public class Mover : MonoBehaviour
{
public float speed = 5f; // shows up in the Inspector!
public int points = 100; // so does this
}
Attach this and the Inspector shows a Speed box (5) and a Points box (100). Type a new number and the script uses it immediately. This lets designers tune the game without reading code.
✅ Pro Tip
Values you set in the Inspector override the value written in the script. If your script says speed = 5f but the Inspector shows 12, the game uses 12. The Inspector wins — handy, but remember it when a value seems "wrong."
public vs [SerializeField]
Making everything public works, but public also lets other scripts change the value, which can cause bugs. The tidy professional habit is to keep the variable private but still show it in the Inspector using [SerializeField]:
[SerializeField] private float speed = 5f;
// Private (safe from other scripts) but still editable in the Inspector.
For this course, either style is fine — just know that [SerializeField] private is what you'll see in most professional code.
Hands-on Exercise
🏋️ Exercise: Tunable values
- Create a script
Moverwith apublic float speed = 5f;. - In
Start(), print it:Debug.Log("Speed is " + speed); - Attach it, then in the Inspector change Speed to
12. - Press Play and read the Console — it prints
Speed is 12, proving the Inspector value won.
✅ Success check
The Console shows the Inspector's number (12), not the script's 5. You've just seen public fields in action.
🎯 Quick Quiz
Question 1: Which type should you use for a decimal speed value?
Question 2: You set Speed to 12 in the Inspector, but the script declares speed = 5f. What value runs?
Summary
🎉 Key Takeaways
- A variable stores a typed value; declare it with type + name.
- Everyday types:
int,float(addf!),string,bool. - public fields appear in the Inspector; the Inspector value overrides the script default.
- [SerializeField] private is the tidy way to expose a value while keeping it protected.
🚀 What's Next?
Now let's actually move something. Lesson 3.3 uses code to make an object glide and spin — with a crucial trick for smooth motion.