Debug.Log() / Debug.LogError()
How to use Debug.Log() to print game object states and variable values to the console in Unity, along with Debug.LogWarning() for warnings and Debug.LogError() for errors.
Syntax
using UnityEngine; // Outputs a normal message in white. Debug.Log(object message); // Associates a specific object as the context. Debug.Log(object message, Object context); // Outputs a warning message in yellow. Debug.LogWarning(object message); // Outputs an error message in red. Debug.LogError(object message); // Outputs an error if the condition is false. Debug.Assert(bool condition, object message); // Clears the console (editor only). Debug.ClearDeveloperConsole();
Method List
| Method | Description |
|---|---|
| Debug.Log(message) | Outputs a normal debug message to the console in white. |
| Debug.LogWarning(message) | Outputs a warning message to the console in yellow. Execution continues. |
| Debug.LogError(message) | Outputs an error message to the console in red. Execution continues (no exception is thrown). |
| Debug.LogException(exception) | Outputs an Exception object as an error. The stack trace is also displayed. |
| Debug.Assert(condition, message) | Outputs message as an error if condition is false. |
| Debug.DrawLine(start, end, color) | Draws a debug line in the Scene view (visible for one frame only). |
Sample Code
using UnityEngine;
public class DebugSample : MonoBehaviour
{
// Set the initial health values.
[SerializeField] private int hp = 100;
[SerializeField] private int maxHp = 100;
void Start()
{
// Basic log output. String interpolation works here too.
Debug.Log($"Game start: health = {hp}");
// Logs object info. Clicking it in the console highlights the object.
Debug.Log($"This object: {gameObject.name}", this);
}
void Update()
{
// Logging every frame produces a flood of output, so use conditions to limit it.
if (Input.GetKeyDown(KeyCode.Space))
{
Debug.Log("Space key was pressed.");
}
}
// Method called when taking damage.
public void TakeDamage(int amount)
{
if (amount <= 0)
{
// Warn when an unexpected value is received.
Debug.LogWarning($"Damage amount is 0 or less: {amount}");
return;
}
hp -= amount;
Debug.Log($"Damage: -{amount} / Remaining health: {hp}");
if (hp <= 0)
{
Debug.LogError("Health reached 0! Check the game over logic.");
}
// Use Assert to detect invalid state.
Debug.Assert(hp <= maxHp, $"Health exceeds max health: {hp} > {maxHp}");
}
}
Notes
In Unity, Debug.Log() outputs only to the editor console, so forgetting to remove it in a production build will not affect game behavior. However, a large number of Debug.Log() calls can impact performance. Keep usage inside Update(), which runs every frame, to a minimum.
To completely strip Debug.Log() calls at build time, wrap them in the #if UNITY_EDITOR preprocessor directive, or set Debug.unityLogger.logEnabled = false in release builds. For finding GameObjects and getting components, see GameObject.Find() / GetComponent<T>().
If you find any errors or copyright issues, please contact us.