0
0
Unityframework~5 mins

Debug.Log for debugging in Unity

Choose your learning style9 modes available
Introduction

Debug.Log helps you see what your program is doing while it runs. It prints messages to the console so you can find and fix problems easily.

You want to check if a part of your game code is running.
You want to see the value of a variable during the game.
You want to find out why something is not working as expected.
You want to track the flow of your game logic step-by-step.
You want to test small parts of your code without stopping the game.
Syntax
Unity
Debug.Log(message);

message can be text or a variable you want to show.

This prints the message to the Unity Console window.

Examples
Prints a simple text message to the console.
Unity
Debug.Log("Hello, world!");
Shows the value of the variable score in the message.
Unity
int score = 10;
Debug.Log("Score is: " + score);
Prints the player's position in the game world.
Unity
Debug.Log(player.transform.position);
Sample Program

This program prints a message when the game starts showing the number of lives. It also prints a message every time you press the space key.

Unity
using UnityEngine;

public class DebugExample : MonoBehaviour
{
    void Start()
    {
        int lives = 3;
        Debug.Log("Game started with lives: " + lives);
    }

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            Debug.Log("Space key pressed!");
        }
    }
}
OutputSuccess
Important Notes

Debug.Log messages appear in the Unity Console window during play mode.

Use Debug.Log to understand what your code is doing without stopping the game.

Remember to remove or comment out Debug.Log calls in the final game to keep the console clean.

Summary

Debug.Log prints messages to the Unity Console to help you see what your game is doing.

Use it to check variable values and game events while running your game.

It is a simple and powerful tool for finding and fixing problems.