🐞 Lesson 3.4: Debugging with the Console
Every developer writes code that breaks — constantly. The skill isn't avoiding errors, it's reading them. The Console is where Unity tells you exactly what went wrong. Let's learn to listen.
🎯 Learning Objectives
- Open and read the Console window
- Tell apart logs, warnings, and errors
- Use
Debug.Logto inspect what your code is doing - Decode and fix the most common beginner errors
Estimated Time: 30 minutes
In This Lesson
Meet the Console
Open it via Window ▸ General ▸ Console (or click the Console tab next to Project). Here's what it looks like with a log, a warning, and an error:
PlayerMovement.cs:17.Logs, Warnings, Errors
| Type | Colour / icon | Meaning |
|---|---|---|
| Log | Grey ⓘ | Information you (or Unity) printed. Harmless. |
| Warning | Yellow ⚠ | "This might be a problem." The game still runs. |
| Error | Red ⛔ | Something broke. Often stops that code from working. Fix these first. |
💡 Double-click an error and Unity jumps straight to the offending line in your code editor. The ...cs:17 at the end of the message is the file and line number — your treasure map.
Debugging with Debug.Log
When code misbehaves but doesn't error, sprinkle in Debug.Log to see what's really happening — the values, and whether a line even runs:
void Update()
{
Debug.Log("Update is running. Speed = " + speed);
if (speed > 10f)
{
Debug.Log("Speed is high!");
}
}
If "Update is running" never prints, the script isn't attached or the object is inactive. If "Speed is high!" never prints, speed isn't above 10. This "print and check" habit solves most beginner mysteries.
✅ Pro Tip
Combine text and values with +: Debug.Log("Score: " + score). Seeing the actual number is far faster than guessing.
Common Beginner Errors (and Fixes)
⚠️ NullReferenceException
"Object reference not set to an instance of an object." You tried to use something that isn't there (a variable you never assigned). Check the line in the message — did you forget to drag an object into a public slot in the Inspector?
⚠️ "; expected" or "} expected"
A typo — a missing semicolon or a mismatched curly brace. Go to the file and line shown and look just before it.
⚠️ "cannot convert double to float"
You wrote a decimal without the f. Change 5.0 to 5.0f.
⚠️ The script won't attach / "can't add script"
The file name and class name don't match, or the file has a compile error. Fix the red errors in the Console first — nothing works until the code compiles cleanly.
Hands-on Exercise
🏋️ Exercise: Break it on purpose
- In any script, delete a semicolon and save. Watch the red error appear in the Console — read the file and line.
- Double-click the error; Unity opens your editor at the exact spot. Fix it.
- Add
Debug.Log("reached here");at the top ofUpdate(), press Play, and confirm it prints every frame.
✅ What this teaches
Errors aren't scary once you know they point to a file and line. Reading the message and jumping to the line is 90% of debugging.
🎯 Quick Quiz
Question 1: Which Console message type means "something broke, fix it first"?
Question 2: An error ends with PlayerMovement.cs:17. What is 17?
Summary
🎉 Key Takeaways
- The Console shows logs (grey), warnings (yellow), and errors (red).
- Fix red errors first — nothing runs until the code compiles.
- The file:line at the end of an error is where to look; double-click to jump there.
- Debug.Log is your flashlight for seeing values and whether code runs.
🚀 What's Next?
You can write, run, and debug scripts. Now let's put the player in control. Module 4 is all about reading input and moving a character.
🎉 Module 3 complete!
You can code — and, just as importantly, fix code. That's a real developer skill.