Wednesday, August 31, 2022
HomeWeb DevelopmentOptimizing your Unity venture with Debug.Log

Optimizing your Unity venture with Debug.Log


So that you need to construct a sport utilizing the Unity engine? You’ve sat down and designed the sport that you simply’d prefer to create, put collectively a bit venture administration board, and also you’ve began implementing the logic wanted to drive your sport.

You’ve hit the Play button a number of instances and examined a number of of the scripts that you simply’ve created for the bottom performance and now that you simply’re engaged on one thing a bit extra advanced, you’ve hit Play and your IDE (Built-in Growth Surroundings) hasn’t thrown any errors your means.

My Code Works But How Gandalf Meme

Every part appears to be like good and the sport runs, however one thing isn’t fairly proper. You’re not getting the information that you simply’re anticipating to get otherwise you’re simply not getting the outcomes that you simply want. This may be tremendous irritating as a Unity newbie, however this text will assist you get via the method of debugging your Unity venture utilizing the Debug.Log assertion.

We’ll be operating via the totally different ways in which you should use the Debug.Log assertion, formatting the output to be extra readable and a number of methods to make sure that you’re utilizing the Debug.Log assertion successfully.

For this text, I’ll be assuming that you simply’re aware of the Unity engine interface and have already written your first script. We’ll be making a quite simple venture with which to exhibit using the statements, however I’ll be including screenshots and an evidence of the method as we go alongside.

Organising the bottom venture

For this text, I’ll be making a 3D venture in Unity 2021.3.4f1, however the model of the Unity engine that you simply use is not going to matter. I’ll even be utilizing VS Neighborhood 2019, so you might discover a number of interface variations should you’re utilizing VS Code or the Rider IDE, however the ideas demonstrated right here will probably be relevant to any IDE.

Within the scene beneath, I’ve received a aircraft and some primitive objects. Every of those objects has a singular identify (their primitive sort), remodel, and script. They are going to be numbered 1 (Sphere), 2 (Dice), 3 (Capsule), and 4 (Cylinder), respectively.

Objects With Names

Subsequent, I created a folder for my scripts, a GameManager script and a GameManager sport object throughout the Hierarchy which is able to comprise the GameManager script that I’ve simply created.

GameManager Script Object

I then dragged or assigned the GameManager.cs script to the sport object that I created within the Hierarchy, which is able to run after we hit the play button. Now that we’re all arrange, we are able to take a look at the essential utilization of the Debug.Log assertion.

Script to Game Object

use the Debug.Log assertion in Unity

Now that now we have our GameManager script assigned to the GameManager sport object inside our Check Scene, we are able to insert our first Debug.Log assertion to check that it runs after we want it to.

The syntax for the Debug.Log assertion is as seen within the display to the correct. Right here I’ve merely said “Debug.Log(”Hiya!);” and since I’ve inserted this assertion into our Begin methodology, this could run as soon as after we run our sport:

utilizing System.Collections;
utilizing System.Collections.Generic;
utilizing UnityEngine;

public class GameManager : MonoBehaviour
{
    // Begin known as earlier than the primary body replace
    void Begin()
    {
        Debug.Log("Hiya!");
    }

    // Replace known as as soon as per body
    void Replace()
    {

    }
}

If we go forward and hit the Play button within the Unity Editor, you’ll see phrase “Hiya!” printed within the Editor Console window.

Hello World Unity Editor


Extra nice articles from LogRocket:


We at the moment are in a position to output easy textual content to the console utilizing the Debug.Log assertion, but it surely’s nonetheless not likely that useful to us when debugging. So let’s strive displaying the worth of a sure variable that we’ve created after we begin up our sport.

To do that, I’ve created a non-public integer known as testValue (as seen beneath) inside our GameManager.cs script and I’ve assigned a worth to it throughout the Begin perform. You may additionally notice that I’m able to create a extra advanced Debug.Log assertion by combining the textual content and the testValue and separating it with a + signal. You’ll additionally discover that I’ve added an area character between the final character of the straightforward textual content and the variable to create some area within the Console window:

utilizing System.Collections;
utilizing System.Collections.Generic;
utilizing UnityEngine;

public class GameManager : MonoBehaviour
{
    personal int testValue;

    // Begin known as earlier than the primary body replace
    void Begin()
    {
        testValue = 1000;
        Debug.Log("That is the worth of testValue: " + testValue);
    }

    // Replace known as as soon as per body
    void Replace()
    {

    }
}

Let’s save our script and head again to the Unity editor to see what the output within the console will probably be.

TestValue 1000

Nice! We are able to now see the assigned worth of testValue, in addition to the little little bit of textual content we’ve added for us to raised perceive the output within the Console window.

Utilizing the Debug.Log assertion to find out the identify of a sport object

Let’s create a reference to a sport object inside our script, connect it to the script throughout the Unity Editor, after which use the Debug.Log assertion to output the identify of that object:

utilizing System.Collections;
utilizing System.Collections.Generic;
utilizing UnityEngine;

public class GameManager : MonoBehaviour
{
    public GameObject testObject;

    void Awake()
    {
        Debug.Log(testObject.identify);
    }
}

When you’ve connected the item to the sport object area on the GameManager.cs script, throughout the Unity Editor, you’ll have the ability to hit Play and the secret object that’s being referenced will probably be proven within the editor.

Name of Object Shown

Utilizing the Debug.Log assertion to find out the worth of a conditional

We might additionally use the Debug.Log assertion to find out the worth of a conditional or a Boolean. Let’s create two integer values after which examine whether or not or not they’re the identical. To do that, I’ve declared and initialized two integers (a and b) and used a Debug.Log assertion to examine if they’re equal:

utilizing System.Collections;
utilizing System.Collections.Generic;
utilizing UnityEngine;

public class GameManager : MonoBehaviour
{
    personal int a = 51;
    personal int b = 62;

    void Awake()
    {
        Debug.Log(a == b);
    }
}

Because the a shouldn’t be the identical as b, the output of the console is as anticipated.

False Message

Utilizing the Debug.Log assertion to find out when a perform is being known as

Within the earlier part, we discovered concerning the syntax for a easy Debug.Log assertion and added a variable worth to the assertion. We had been additionally in a position to see the output to the console. On this part, I’ll be trying on the timing of sure Unity features and the right way to inform when a customized perform has been known as.

The next is taken from the official Unity documentation on the order of execution for occasion features.

Unity script features will execute in an outlined order when your script is run and it may be actually useful to see when a particular occasion or perform is triggered, when debugging. Let’s take a look on the following circulation diagram, offered by Unity.

From the diagram beneath, we are able to see that the Awake perform is the primary perform to be known as when a Unity script is run. We then have the OnEnable and Begin strategies operating sequentially.

Diagram Showing Functions

Let’s check this order with a easy script and the insertion of some Debug.Log statements. I’ve added a testValue integer after which personalized every Debug.Log assertion in order that we all know the place every of them is being run. The assertion above every Debug.Log assertion merely increments the worth of testValue:

utilizing System.Collections;
utilizing System.Collections.Generic;
utilizing UnityEngine;

public class GameManager : MonoBehaviour
{
    personal int testValue = 0;

    personal void Awake()
    {
        testValue++;
        Debug.Log("This was run within the Awake methodology: " + testValue);
    }

    personal void OnEnable()
    {
        testValue++;
        Debug.Log("This was run within the OnEnable methodology: " + testValue);
    }

    personal void Begin()
    {
        testValue++;
        Debug.Log("This was run within the Begin methodology: " + testValue);
    }
}

Let’s see what occurs within the Console window after we hit the Play button.

Nice! We are able to see that the Awake methodology executes first, the OnEnable methodology subsequent after which the Begin methodology. We are able to additionally see that every of those strategies solely runs as soon as after we hit the Play button.

Three Messages

Now let’s create a customized perform known as OurOwnFunction, which is able to comprise a Debug.Log assertion of its personal. We’ll name this perform in two of the opposite Unity features and see what the output is:

utilizing System.Collections;
utilizing System.Collections.Generic;
utilizing UnityEngine;

public class GameManager : MonoBehaviour
{
    personal int testValue = 0;

    personal void Awake()
    {
        testValue++;
        Debug.Log("This was run within the Awake methodology: " + testValue);
        OurOwnFunction();
    }

    personal void OnEnable()
    {
        testValue++;
        Debug.Log("This was run within the OnEnable methodology: " + testValue);
    }

    personal void Begin()
    {
        testValue++;
        Debug.Log("This was run within the Begin methodology: " + testValue);
        OurOwnFunction();
    }

    personal void OurOwnFunction()
    {
        Debug.Log("That is the customized perform name!");
    }
}

The output within the Console is proven beneath and we are able to now see that our customized perform known as within the Awake and Begin features, respectively.

Five Messages

Different kinds of debug messages

Now, we’ve checked out methods wherein we are able to use the Debug.Log assertion, however there are additionally different classes of debug statements that we might use. These statements are utilized in the identical means that the Debug.Log assertion is used however permit us to show warnings or errors within the Unity Console:

utilizing System.Collections;
utilizing System.Collections.Generic;
utilizing UnityEngine;

public class GameManager : MonoBehaviour
{
    void Begin()
    {
        Debug.Log("This can be a regular log.");
        Debug.LogWarning("This can be a warning message.");
        Debug.LogError("That is an error message.");
    }
}

And right here now we have the output of the above.

Output Messages

In conclusion

Utilizing the Debug assertion could be very useful in seeing what the worth of sure variables or objects are at runtime, figuring out whether or not or not a perform is definitely operating how and once you imply it to and even simply monitoring down a pesky bug.

I hope that you’ve got discovered this handy and are ready to make use of Debug statements extra successfully inside your venture. Simply keep in mind to take away them or flip them into feedback once they’re now not in use and also you’re all set to debug your means out of your subsequent huge sport.

: Full visibility into your internet and cellular apps

LogRocket is a frontend utility monitoring answer that allows you to replay issues as in the event that they occurred in your individual browser. As a substitute of guessing why errors occur, or asking customers for screenshots and log dumps, LogRocket allows you to replay the session to shortly perceive what went flawed. It really works completely with any app, no matter framework, and has plugins to log extra context from Redux, Vuex, and @ngrx/retailer.

Along with logging Redux actions and state, LogRocket information console logs, JavaScript errors, stacktraces, community requests/responses with headers + our bodies, browser metadata, and customized logs. It additionally devices the DOM to report the HTML and CSS on the web page, recreating pixel-perfect movies of even essentially the most advanced single-page internet and cellular apps.

.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments