Programming Errors – Unity Debug.Log

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.

Fig. 1: Initial scene to study the Debug.Log method.

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.

Fig. 2: Console tab in Unity 3D.

If the console window is not active, you can open it from the Window tab as shown in figure 3.

Fig. 3: Path to open the console in Unity 3D.

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”.

Click here to know more about Scripts in Programming

Fig. 4: Create new C# Script.
Fig. 5: The C# script is called 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).

Fig. 6: Create new Empty GameObject in the hierarchy.
Fig. 7: We give the GameObject a name.
Fig. 8: The Script created previously in the inspector is assigned.
Fig. 9: The Debugging Script has been assigned in the inspector.

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.

In my channel there is a video about what a programming method is and also an article on this page.

Fig. 10: Default content of a C# script.

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”.

Fig. 11: We write the Debug.Log instruction in the Start method.

We enter the game mode to test the Script, for this we use the Play button of the Editor (figure 12).

Fig. 12: Button to enter the game mode.

Fig. 13: In the lower left corner of the program the console messages also appear.

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.

Fig. 14: Message in console, on the right appears a number indicating the number of times the message is sent.

Let’s see what happens if we put a message in the Update method.

Fig. 15: Another message is placed 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.

Fig. 16: Capturing messages in the first seconds of the game.
Fig. 17: Capturing the messages a few moments later.

Using Variables

The messages that we want to print in console can be stored in variables and the result is the same.

Fig. 18: Two strings are defined with the messages and placed in the methods.

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.

In the channel there is a video about “What is a Variable in Programming” and I also wrote an article for this page.

Fig. 19: A variable is defined as integer, bool and float, each with values.

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.

Fig. 20: Debug.Log is executed with the new variables.

When you enter game mode, all four messages appear on the console.

Fig. 21: The console displays the value of each variable.

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.

Fig. 22: Execution of ToString() that converts the value of the variable into text.

We are going to define 4 new variables, one of each type and give it different values, as shown in figure 23.

Fig. 23: More variables are defined to perform operations.

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.

Fig. 24: Operations are entered in the parameter of the Debug.Log methods.

Fig. 25: The results of these operations are printed on the console.

It is also possible to concatenate different types of variables and print a message.

Fig. 26: Different types of variable can be concatenated to form the message.
Fig. 27: Message made up of different types of variables.

Frame Counter

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.

Fig. 28: An integer is defined to count the number of frames that pass.

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.

Fig. 29: A message is printed with the frames count and then the counter is increased.

Figures 30 and 31 show the execution of this code at two different times.

Fig. 30: Messages in the first seconds of execution.
Fig. 31: Messages appearing moments later.

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.

Fig. 32: A vector of integers with different values is defined.

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.

Fig. 33: In the Start method we are going to analyze the array using Debug.Log.

In this way using the console we can study the array and its elements, as can be seen in figure 34.

Fig. 34: Console messages that give us information about the Array.

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.

Fig. 35: A method is defined that returns a string to print message in console.
Fig. 36: The message consists of the string returned by the method.

The same could be done with a method that requires parameters, as shown in figure 37.

Fig. 37: A parameter is added to the method.
Fig. 38: The message depends on the parameter we give to the method.

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.

Fig. 39: Two methods are defined, one of which will be called randomly.

In figures 40 and 41 we see the execution in two different moments of the game.

Fig. 40: In this case the BadPath method was executed.
Fig. 41: In this case the “GoodPath” method was executed.

Conclusion

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.

Exit mobile version
Secured By miniOrange