Programming Errors ‚ÄĒ Unity Debug.Log

Introduction ‚ÄĒ Project Setup

In this arti¬≠cle we will learn how to use con¬≠sole mes¬≠sages to ana¬≠lyze how our code works and even¬≠tu¬≠al¬≠ly detect and cor¬≠rect bugs.

To start we are going to cre­ate a new project in Uni­ty, in my case I am going to call it Debugging.

CHECK OUT THIS CRAZY EXPERIMENT

Fig. 1: Ini­tial scene to study the Debug.Log method.

In the process of detec¬≠tion and cor¬≠rec¬≠tion of bugs we are going to use main¬≠ly the con¬≠sole of Uni¬≠ty, in that win¬≠dow will print mes¬≠sages that we will strate¬≠gi¬≠cal¬≠ly place in our code.

Fig. 2: Con¬≠sole tab in Uni¬≠ty 3D.

If the con¬≠sole win¬≠dow is not active, you can open it from the Win¬≠dow tab as shown in fig¬≠ure 3.

Fig. 3: Path to open the con¬≠sole in Uni¬≠ty 3D.

The next step is to cre­ate a new C# Script, we can do it from the project win­dow as shown in fig­ure 4. I'm going to give it the name "Debug­ging".

Click here to know more about Scripts in Pro­gram­ming

Fig. 4: Cre­ate new C# Script.
Fig. 5: The C# script is called Debugging.

For the code of our Script to run in gen¬≠er¬≠al it is not enough to cre¬≠ate the Script, it must also be assigned to at least one GameOb¬≠ject of the hier¬≠ar¬≠chy. Let's cre¬≠ate an Emp¬≠ty GameOb¬≠ject and give it a mean¬≠ing¬≠ful name.

Then with that GameOb¬≠ject select¬≠ed we drag the Script towards the inspec¬≠tor to assign it (fig¬≠ure 8).

Fig. 6: Cre­ate new Emp­ty GameOb­ject in the hierarchy.
Fig. 7: We give the GameOb¬≠ject a name.
Fig. 8: The Script cre­at­ed pre­vi­ous­ly in the inspec­tor is assigned.
Fig. 9: The Debug­ging Script has been assigned in the inspector.

Programming C# Unity ‚ÄĒ Debug.Log

When open­ing the Script (fig­ure 10) we see that there is some code that is writ­ten by default. The basic libraries are import­ed and we have the Start and Update meth­ods which run automatically.

In my chan­nel there is a video about what a pro­gram­ming method is and also an arti­cle on this page.

Fig. 10: Default con­tent of a C# script.

We are going to write an instruc¬≠tion in the Start method. This instruc¬≠tion is the exe¬≠cu¬≠tion of the Log method of the Debug class, to which we will pass the mes¬≠sage to print.

As can be seen in fig¬≠ure 11, the instruc¬≠tion is:

Debug.Log("Este men­saje apare­cerá en la consola");

Which means: "This mes­sage will appear in console".

Fig. 11: We write the Debug.Log instruc­tion in the Start method.

We enter the game mode to test the Script, for this we use the Play but¬≠ton of the Edi¬≠tor (fig¬≠ure 12).

Fig. 12: But¬≠ton to enter the game mode.

Fig. 13: In the low­er left cor­ner of the pro­gram the con­sole mes­sages also appear.

You can see that the mes¬≠sage appears in the low¬≠er left cor¬≠ner of the pro¬≠gram with¬≠out hav¬≠ing the con¬≠sole open.

In fig­ure 14 I sep­a­rat­ed the con­sole win­dow to bet­ter visu­al­ize the information.

Fig. 14: Mes¬≠sage in con¬≠sole, on the right appears a num¬≠ber indi¬≠cat¬≠ing the num¬≠ber of times the mes¬≠sage is sent.

Let's see what hap­pens if we put a mes­sage in the Update method.

Fig. 15: Anoth­er mes­sage is placed in the Update method.

To the right of the mes­sage is a num­ber indi­cat­ing the num­ber of times the mes­sage is printed.

As can be seen in fig¬≠ure 16, the mes¬≠sage of the Update method has been print¬≠ed 82 at the time of cap¬≠ture while in fig¬≠ure 17 the mes¬≠sage has been print¬≠ed 350 times.

Fig. 16: Cap¬≠tur¬≠ing mes¬≠sages in the first sec¬≠onds of the game.
Fig. 17: Cap­tur­ing the mes­sages a few moments later.

Using Variables

The mes¬≠sages that we want to print in con¬≠sole can be stored in vari¬≠ables and the result is the same.

Fig. 18: Two strings are defined with the mes­sages and placed in the methods.

Let's cre­ate some more vari­ables, an inte­ger, a boolean and a float. These three types of vari­ables togeth­er with the String are the basic types of vari­ables that we are going to use in any project.

In the chan­nel there is a video about "What is a Vari­able in Pro­gram­ming" and I also wrote an arti­cle for this page.

Fig. 19: A vari­able is defined as inte­ger, bool and float, each with values.

As can be seen in fig­ure 19, we are going to declare them and ini­tial­ize them with a val­ue. We can do this in dif­fer­ent points of the Script, but we will do it at the begin­ning of the class, before the Start method.

Then we are going to exe­cute three Debug.Log meth­ods pass­ing as para­me­ter the variables.

Fig. 20: Debug.Log is exe­cut­ed with the new variables.

When you enter game mode, all four mes­sages appear on the console.

Fig. 21: The con­sole dis­plays the val­ue of each variable.

In pre­vi­ous ver­sions it was nec­es­sary to exe­cute the ToString() method in order to print the val­ue of a vari­able that is not a string, as shown in fig­ure 22, this can be done but is no longer nec­es­sary, you can pass the vari­ables directly.

Fig. 22: Exe¬≠cu¬≠tion of ToString() that con¬≠verts the val¬≠ue of the vari¬≠able into text.

We are going to define 4 new vari¬≠ables, one of each type and give it dif¬≠fer¬≠ent val¬≠ues, as shown in fig¬≠ure 23.

Fig. 23: More vari¬≠ables are defined to per¬≠form operations. 

We can make the con¬≠sole print the result of oper¬≠a¬≠tions with vari¬≠ables, as shown in fig¬≠ure 24.

If we com¬≠pare the val¬≠ues in fig¬≠ure 23 with the con¬≠sole prints seen in fig¬≠ure 25, we can ver¬≠i¬≠fy this.

Fig. 24: Oper­a­tions are entered in the para­me­ter of the Debug.Log methods.

Fig. 25: The results of these oper­a­tions are print­ed on the console.

It is also pos­si­ble to con­cate­nate dif­fer­ent types of vari­ables and print a message.

Fig. 26: Dif­fer­ent types of vari­able can be con­cate­nat­ed to form the message.
Fig. 27: Mes­sage made up of dif­fer­ent types of variables.

Frame Counter

Pre¬≠vi¬≠ous¬≠ly we placed a Debug.Log instruc¬≠tion in the Update method, which con¬≠tained a fixed message. 

We are going to print a mes¬≠sage that is going to change frame to frame.

Fig. 28: An inte¬≠ger is defined to count the num¬≠ber of frames that pass.

To start with we define an inte¬≠ger to count the frames and ini¬≠tial¬≠ize it in the val¬≠ue 0.

Let's con­cate­nate the mes­sage "This is the frame: " (includ­ing the final space), with the counter value.

After dis¬≠play¬≠ing the mes¬≠sage in con¬≠sole, we increase the counter so that in the next frame it indi¬≠cates the next frame number. 

These two instruc¬≠tions are shown in Fig¬≠ure 29.

Fig. 29: A mes­sage is print­ed with the frames count and then the counter is increased.

Fig¬≠ures 30 and 31 show the exe¬≠cu¬≠tion of this code at two dif¬≠fer¬≠ent times.

Fig. 30: Mes­sages in the first sec­onds of execution.
Fig. 31: Mes­sages appear­ing moments later.

Elements of an Array or Integer Vector

Now we are going to apply the Debug.Log Method to ana¬≠lyze the ele¬≠ments of an Array.

An Array is an ordered struc­ture of data of some kind upon which oper­a­tions can be made. This data struc­ture is like the math­e­mat­i­cal vector.

First we are going to declare an Array of inte¬≠gers, this is sim¬≠i¬≠lar to declar¬≠ing an inte¬≠ger only by adding the square brack¬≠ets after the type, as seen in Fig¬≠ure 32.

At the same point we are going to cre¬≠ate the Array with the val¬≠ues seen in fig¬≠ure 32.

Fig. 32: A vec­tor of inte­gers with dif­fer­ent val­ues is defined.

We are going to read the Array in the Start method, first we are going to write a mes­sage indi­cat­ing the size of the vector.

To go through the Array we use a "for" loop that starts from 0 to the amount of Array ele¬≠ments minus one.

Inside the for loop we are going to print a mes­sage in con­sole using Debug.Log and con­cate­nat­ing text with the val­ues of the variable.

for loop in c# to detect programming errors
Fig. 33: In the Start method we are going to ana­lyze the array using Debug.Log.

In this way using the con¬≠sole we can study the array and its ele¬≠ments, as can be seen in fig¬≠ure 34.

Fig. 34: Con¬≠sole mes¬≠sages that give us infor¬≠ma¬≠tion about the Array.

Print the result of the execution of a Method

If we have a method that returns a vari¬≠able, we can apply it as a para¬≠me¬≠ter of the Debug.Log method, as shown in fig¬≠ure 35.

The method "UnMeto­doEx­ce­lente" is defined which returns a string.

The exe¬≠cu¬≠tion of this code is shown in fig¬≠ure 36.

Fig. 35: A method is defined that returns a string to print mes­sage in console.
Fig. 36: The mes­sage con­sists of the string returned by the method.

The same could be done with a method that requires para¬≠me¬≠ters, as shown in fig¬≠ure 37.

Fig. 37: A para­me­ter is added to the method.
Fig. 38: The mes­sage depends on the para­me­ter we give to the method.

Debug.Log to analyze the result of an if statement

The flow of our pro¬≠gram can move in dif¬≠fer¬≠ent ways depend¬≠ing on the data, for exam¬≠ple in a sen¬≠tence if, the flow of the pro¬≠gram will take one of two pos¬≠si¬≠ble paths depend¬≠ing on the Boolean vari¬≠able being evaluated. 

In fig­ure 39 we define the if sen­tence, which will exe­cute one method or the oth­er. Each method prints its char­ac­ter­is­tic message.

Fig. 39: Two meth­ods are defined, one of which will be called randomly.

In fig¬≠ures 40 and 41 we see the exe¬≠cu¬≠tion in two dif¬≠fer¬≠ent moments of the game.

Fig. 40: In this case the Bad­Path method was executed.
Fig. 41: In this case the "Good¬≠Path" method was executed. 

Conclusion

The Debug.Log method is used to write mes­sages in the console.

By strate­gi­cal­ly plac­ing these mes­sages in our code we are able to visu­al­ize the flow of the pro­gram, get infor­ma­tion about the state of our vari­ables and objects.

The Debug.Log method takes as para­me­ter a vari­able that can be in prin­ci­ple string, bool, int or float, the basic types of vari­ables that we have ana­lyzed in oth­er articles.

These para­me­ters can be deliv­ered in dif­fer­ent ways, for exam­ple it is pos­si­ble to cre­ate a mes­sage con­cate­nat­ing vari­ables, as the result of a method, enter the vari­ables direct­ly or enter the oper­a­tions whose results we want to be printed.

YouTube
Scroll to Top
Secured By miniOrange