Learn how to debug!
You want to make a game in Unity, but your player is not moving or triggering a collision? Well, in this video, we will learn something about Debugging in Unity.
First of all, debugging is one of the first steps when making a game in Unity. It can determine whether a function is working or a value is changing. Since Unity uses the C# programming language, the syntax used for debugging looks like this – Debug.Log(“message”);
This is the first tutorial after a two month break which happened due to the focus on the Birdiez animation project. You can watch other tutorials here.
Why debug?
When making a game in Unity, there are many things that can go wrong and we don’t know how to fix them. The actual way to fix them is by learning how to properly use Debugging in Unity.
Debugging is important for defining whether a value is increasing or not. It can be also used for detecting collisions or movement. However, Debug has some different ways to write messages in Console.
Debug.LogError is used as a variant of Debug.Log for writing error messages. LogWarning is used for warning messages and LogException requires a try-catch block to tell if a program has an exception.
Another kind of message is called assertion message and is written like this. LogAssertion(object message, Object context); When opening a C# script, if a Debug.Log is put in void Start(), it will be played at first frame only.
How to debug
Debugging is typically used to determine if a value is changing or a collider is working. To make sure that you can move your character, detect collision or add more values, use Debug.Log and write the value without quotation marks.
public int coins;
void Start()
{
Debug.Log(coins); //0 if the value isn't changed
}
Adding the same line into void Update() will display the message every frame, so it’s better to use it only when a value is updated in an if statement. In order to check if your player is moving, use any player movement script that was explained in previous videos and try to add these lines of code. It is not generally required to Debug if everything is working. These are just some experimental lines to detect whether a code is working or not.
For example, the value lives is added in another script as a static int and the debug script is calling it with -= to indicate the reduction of the value.
public static int lives;
And in any other script:
void EnemyHit()
{
GameManager.lives -= 1;
Debug.Log("Current lives: " +lives); //if the main value was 3, it will display 2.
}
Extra tips
Debugging can also be used for drawing lines and rays. DrawLine is used for drawing a simple line with its own width and depth, while DrawRay adds a line to the object and makes sure that the mouse position will follow it.
To make sure that the debugging will clear the console, use Debug.ClearDeveloperConsole(); to clear all the lines. This is a very useful tip to make the performance better and the lines of console will be cleared. Amazing gaming performance is on its way!


No responses yet