Introduction – Project Setup
In this article we will learn how to use console messages to analyze how our code works and eventually detect and correct bugs.
To start we are going to create a new project in Unity, in my case I am going to call it Debugging.
In the process of detection and correction of bugs we are going to use mainly the console of Unity, in that window will print messages that we will strategically place in our code.
If the console window is not active, you can open it from the Window tab as shown in figure 3.
The next step is to create a new C# Script, we can do it from the project window as shown in figure 4. I’m going to give it the name “Debugging”.
For the code of our Script to run in general it is not enough to create the Script, it must also be assigned to at least one GameObject of the hierarchy. Let’s create an Empty GameObject and give it a meaningful name.
Then with that GameObject selected we drag the Script towards the inspector to assign it (figure 8).
Programming C# Unity – Debug.Log
When opening the Script (figure 10) we see that there is some code that is written by default. The basic libraries are imported and we have the Start and Update methods which run automatically.
We are going to write an instruction in the Start method. This instruction is the execution of the Log method of the Debug class, to which we will pass the message to print.
As can be seen in figure 11, the instruction is:
Debug.Log(“Este mensaje aparecerá en la consola”);
Which means: “This message will appear in console”.
We enter the game mode to test the Script, for this we use the Play button of the Editor (figure 12).
You can see that the message appears in the lower left corner of the program without having the console open.
In figure 14 I separated the console window to better visualize the information.
Let’s see what happens if we put a message in the Update method.
To the right of the message is a number indicating the number of times the message is printed.
As can be seen in figure 16, the message of the Update method has been printed 82 at the time of capture while in figure 17 the message has been printed 350 times.
The messages that we want to print in console can be stored in variables and the result is the same.
Let’s create some more variables, an integer, a boolean and a float. These three types of variables together with the String are the basic types of variables that we are going to use in any project.
As can be seen in figure 19, we are going to declare them and initialize them with a value. We can do this in different points of the Script, but we will do it at the beginning of the class, before the Start method.
Then we are going to execute three Debug.Log methods passing as parameter the variables.
When you enter game mode, all four messages appear on the console.
In previous versions it was necessary to execute the ToString() method in order to print the value of a variable that is not a string, as shown in figure 22, this can be done but is no longer necessary, you can pass the variables directly.
We are going to define 4 new variables, one of each type and give it different values, as shown in figure 23.
We can make the console print the result of operations with variables, as shown in figure 24.
If we compare the values in figure 23 with the console prints seen in figure 25, we can verify this.
It is also possible to concatenate different types of variables and print a message.
Previously we placed a Debug.Log instruction in the Update method, which contained a fixed message.
We are going to print a message that is going to change frame to frame.
To start with we define an integer to count the frames and initialize it in the value 0.
Let’s concatenate the message “This is the frame: ” (including the final space), with the counter value.
After displaying the message in console, we increase the counter so that in the next frame it indicates the next frame number.
These two instructions are shown in Figure 29.
Figures 30 and 31 show the execution of this code at two different times.
Elements of an Array or Integer Vector
Now we are going to apply the Debug.Log Method to analyze the elements of an Array.
An Array is an ordered structure of data of some kind upon which operations can be made. This data structure is like the mathematical vector.
First we are going to declare an Array of integers, this is similar to declaring an integer only by adding the square brackets after the type, as seen in Figure 32.
At the same point we are going to create the Array with the values seen in figure 32.
We are going to read the Array in the Start method, first we are going to write a message indicating the size of the vector.
To go through the Array we use a “for” loop that starts from 0 to the amount of Array elements minus one.
Inside the for loop we are going to print a message in console using Debug.Log and concatenating text with the values of the variable.
In this way using the console we can study the array and its elements, as can be seen in figure 34.
Print the result of the execution of a Method
If we have a method that returns a variable, we can apply it as a parameter of the Debug.Log method, as shown in figure 35.
The method “UnMetodoExcelente” is defined which returns a string.
The execution of this code is shown in figure 36.
The same could be done with a method that requires parameters, as shown in figure 37.
Debug.Log to analyze the result of an if statement
The flow of our program can move in different ways depending on the data, for example in a sentence if, the flow of the program will take one of two possible paths depending on the Boolean variable being evaluated.
In figure 39 we define the if sentence, which will execute one method or the other. Each method prints its characteristic message.
In figures 40 and 41 we see the execution in two different moments of the game.
The Debug.Log method is used to write messages in the console.
By strategically placing these messages in our code we are able to visualize the flow of the program, get information about the state of our variables and objects.
The Debug.Log method takes as parameter a variable that can be in principle string, bool, int or float, the basic types of variables that we have analyzed in other articles.
These parameters can be delivered in different ways, for example it is possible to create a message concatenating variables, as the result of a method, enter the variables directly or enter the operations whose results we want to be printed.