【Unity】Unity Debugging Fundamentals - Debug.Log and Error Handling

Created: 2025-12-07

Bugs are friends, not enemies! Learn Unity's most fundamental debugging techniques: using Debug.Log, interpreting common error messages, and advanced debugging with the debugger.

Overview

Bugs are inevitable in programming. No matter how carefully you write code, unexpected errors and unintended behavior will occur. Great developers aren't those who never write bugs—they're those who efficiently find and fix them. This bug-finding process is called debugging.

Unity provides several powerful tools for debugging. This article covers the basics every beginner should know: the simplest debugging method Debug.Log, interpreting Unity's console error messages, and basic usage of the debugger for more advanced debugging.

The Simplest Debugging Tool: Debug.Log()

Debug.Log() outputs specified messages to Unity Editor's Console window. It's the simplest and most effective way to confirm whether specific code sections execute and to check variable values at that point.

using UnityEngine;

public class DebugExample : MonoBehaviour
{
    private int score = 0;

    void Start()
    {
        // Output message when game starts
        Debug.Log("Game has started.");
    }

    void Update()
    {
        // Check if button was pressed
        if (Input.GetKeyDown(KeyCode.Space))
        {
            score += 10;
            // Output current score variable value
            Debug.Log("Space key pressed. Current score: " + score);
        }
    }
}

Open the Console window via Window > General > Console in Unity Editor.

Debug.LogWarning() and Debug.LogError()

The Debug class has variations for different message severity levels:

  • Debug.LogWarning(): Outputs warning messages (yellow icon). Use for non-error but noteworthy situations (e.g., unset options).
  • Debug.LogError(): Outputs error messages (red icon). Use for critical problems that prevent proper execution.

Using these appropriately organizes console logs and helps quickly assess problem severity.

Interpreting Error Messages

When programs have problems, Unity displays red error messages in the Console. While overwhelming for beginners, these messages contain important hints for problem-solving.

Common Error: NullReferenceException

Known as "null pointer exception," this is among the most frequently encountered errors. It occurs when "trying to access methods or properties of a non-existent (null) object."

Common Causes:

  • GetComponent<T>() tries to get a component that isn't actually attached.
  • A public variable that should be set in Inspector is empty.
// Example: Trying to access Rigidbody that isn't attached
public class NullRefExample : MonoBehaviour
{
    private Rigidbody rb;

    void Start()
    {
        // rb gets nothing assigned here
    }

    void FixedUpdate()
    {
        // rb is null, so NullReferenceException occurs here
        rb.AddForce(Vector3.up);
    }
}

Solution: Double-click the error message to jump to the code line. Check whether variables used on that line are null using Debug.Log or if (variable != null) to identify the cause.

Step Execution with the Debugger

Debug.Log is convenient, but sometimes you want to stop program execution and observe all variable states at that moment. The debugger enables such advanced debugging.

Code editors like Visual Studio and Rider have built-in debuggers that integrate with Unity.

Basic Usage:

  1. Set Breakpoints: Click left of a code line in your editor to set a red circle (breakpoint).
  2. Attach to Unity: Press the "Attach to Unity" button (usually a play button icon) to connect the debugger to Unity Editor.
  3. Run the Game: Play the game in Unity Editor.
  4. Execution Pauses: When execution reaches a breakpoint line, it stops there.
  5. Inspect Variables and Step: While stopped, the debug window shows variable values. Use "Step Over" (advance one line) or "Step Into" (enter functions) to trace program flow line by line.

Mastering the debugger takes practice, but it's the ultimate weapon for solving complex bugs.

Summary

Debugging is a trial-and-error process. Don't fear errors—treat them as clues for problem-solving.

  • Use Debug.Log actively: Casually check variable values and execution flow.
  • Read error messages carefully: They're treasure maps showing problem types and locations.
  • Watch for NullReferenceException: Always be conscious of whether objects and components are correctly set/retrieved.
  • Try the debugger: When facing complex problems, peek inside program execution.

Developing these debugging skills dramatically improves development speed and quality.