Ways To Log And Customize Debug Messages For Unity Console
The most basic thing you can do in programming is print a string onto the console. Unity is the same: You can write a string to Unity Editor’s console, and there are different types of log messages and ways you can customize to make debugging easier.
The most common logging method is the Debug.Log() method which prints a regular string onto the console in Unity in the default text color. There are also 2 other types of log types called Debug.LogWarning() and Debug.LogError() that prints a string in yellow and red, respectively.
Knowing how to use the logging methods and how they interact with the Unity console will make your life much easier. Read more below to learn how to customize debug messages.
Table of Contents
- Debug.Log
- Debug.LogWarning
- Debug.LogError
- Debug.LogException
- Format Debug Log Messages with Rich Text Markup
- Composite Formatting
- Ending Thoughts
Debug.Log
The Debug.Log is the most basic and the easiest way to print a value to the Unity console.
Simply supply it with a string or any value that can be converted into a string and it will display the message on the console.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TestScript : MonoBehaviour
{
private void Start()
{
Debug.Log("This string will be printed to the console.");
}
}
Code language: C# (cs)
The result will look something like this in the console:
Clicking on the message will bring up a small details section that tells you about where the message comes from.
For example, the message in the image above is at line number 9 in the script named TestScript.
Additionally, the latest console message will also display at the bottom left corner of Unity Editor.
The Debug.Log method can print the value of anything as long as it can be converted into a string.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TestScript : MonoBehaviour
{
private void Start()
{
float someNumber = 420.69f;
bool isVeryCool = true;
Debug.Log(someNumber);
Debug.Log(someNumber + " BLAZE IT");
Debug.Log(isVeryCool);
}
}
Code language: C# (cs)
The code above will print 3 lines of messages to the Unity console:
You can also link a related game object to a console message to make it easier to find where the message comes from by passing an object as the second parameter of the Debug.Log method.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TestScript : MonoBehaviour
{
private void Start()
{
int someNumber = 1234;
Debug.Log("This is a number: " + someNumber, transform);
}
}
Code language: C# (cs)
This will highlight the associated object in the hierarchy window when you click on the message in the console window.
Aside from logging messages to the Unity console, Debug.Log (and other logging methods) also logs messages to log files of your game. Information on where to find these log files for each platform can be found here.
Debug.LogWarning
The Debug.LogWarning works the same way as Debug.Log but the message printed will have a warning icon and the color of the message on the bottom left corner of the Unity Editor will be yellow instead.
This one is generally used to give a warning that something might not be working correctly or something is missing from the code but can still work. Something like that. You get the idea.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TestScript : MonoBehaviour
{
private void Start()
{
Debug.LogWarning("Setting the width to more than 300px will result in a drop in performance.");
}
}
Code language: C# (cs)
This will log a message with a warning icon to the console.
The warning message shown at the bottom left corner of Unity Editor will be yellow.
An object can also be passed to the method to link the object with the warning message, just like Debug.Log.
Debug.LogError
The Debug.LogError method behaves in the same way as the Debug.Log method and the Debug.LogWarning method, but the message logged to the Unity console will have an error icon and the message at the bottom of the editor will be red.
This logging method is commonly used for displaying code-breaking error messages to the console.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TestScript : MonoBehaviour
{
private void Start()
{
Debug.LogError("Failed to initialize player's default parameters.");
}
}
Code language: C# (cs)
The message printed to the console will have an error icon attached to it.
The message shown at the bottom left corner of Unity Editor will have a dark red text color.
An object can also be passed to the method to link the object with the error message, just like Debug.Log.
Debug.LogException
The Debug.LogException is used to log exceptions to the Unity console. It works the same way as the regular Debug.LogError except you supply it with an exception object instead of a string.
You use this when you want the error to be more specific, or you want to capture an exception and print it to the console manually.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TestScript : MonoBehaviour
{
private void Start()
{
Debug.LogException(
new MissingComponentException(
"RigidBody2D is missing in " + transform.gameObject.name +
". RigidBody2D is required for this script to work properly."
),
transform
);
}
}
Code language: C# (cs)
This will log an exception to the console with an error icon.
This message will display as dark red text at the bottom left corner of the Unity Editor.
It might be a little hard to see as I’m using dark mode, but it’s there.
Format Debug Log Messages with Rich Text Markup
All Debug.Log variants can be further customized using rich text markup. You can do various things such as making a word bold or changing the color of a message.
The rich text markup’s syntax is similar to HTML markup but not entirely the same.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TestScript : MonoBehaviour
{
private void Start()
{
Debug.Log("I do <b>NOT</b> like ducks.");
Debug.LogWarning("Something <i>weird</i> might happen. Be careful.");
Debug.LogError("This function stopped working <b><i>again</i></b>.");
Debug.Log("<color=red>Red</color>/<color=blue>Blue</color>/<color=green>Green</color>");
}
}
Code language: C# (cs)
If you’re familiar with HTML, this one should be easy to understand. For more information about the rich text, visit the Unity Rich Text Official Manual page.
Composite Formatting
All Debug.Log variants come with Format versions of themselves which allow more complex formatting called Composite Formatting.
These methods are:
- Debug.LogFormat
- Debug.LogWarningFormat
- Debug.LogErrorFormat
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TestScript : MonoBehaviour
{
private void Start()
{
string[] names = { "Chloe", "Angelica", "Yufine", "Tamarinne" };
float[] money = { 1250f, 777f, 240.5723f, 300f };
for (int i = 0; i < names.Length; i++)
{
Debug.LogFormat("{0} | {1:C2}", names[i], money[i]);
}
}
}
Code language: C# (cs)
For more information about composite formatting, refer to MSDN documentation on Composite Formatting.
Ending Thoughts
Knowing how to log messages to the console in different ways can make your debugging process a lot easier, both to yourself and to those who work with you.
I recommend trying those logging methods out yourself in the editor so it’s easier to understand.
I hope this has been a good read for you. Leave a comment if you have suggestions or I’m missing something. And I’ll see you again in the next article!
Cheers!
Attribution
Console icon made by Freepik from www.flaticon.com