Unity - The Console



The Console is where we will be reading the Developer outputs. These outputs can be used to quickly test bits of code without having to give added functionality for testing.

There are three types of messages that appear in the default console. These messages can be related to most of the compiler standards −

  • Errors
  • Warnings
  • Messages

Errors

Errors are issues or exceptions that will prevent the code from running at all.

Warnings

Warnings are issues that will not stop your code from running, but may pose issues during runtime.

Messages

Messages are outputs that convey something to the user; they do not usually highlight issues.

We can even have the Console output our own messages, warnings and errors. To do so, we will use the Debug class. The Debug class is a part of MonoBehaviour, which gives us methods to write messages to the Console, quite similar to how you would create normal output messages in your starter programs.

You can find the Console in the labelled tab above the Assets region.

Console

The outputs of the console are more useful to the programmer, not the end user or player.

Let us try writing a simple message to the Console. This will notify us when the Space key was pressed. For this, we will use the Log method, which takes in an Object as a parameter, which we will use a string in.

You can start with a fresh script or modify an existing one.

void Update() {
   if (Input.GetKeyDown(KeyCode.Space))
      Debug.Log(“Space key was pressed!”);
}

Saving, compiling and running this code (by attaching it to a GameObject, of course), try to hit the spacebar.

Note − Observe that the message shows up at the bottom of the editor.

spacebar Hit

If you click on the Console tab, you will find your message printed out.

Similarly, you can also output warnings by using the LogWarning method and errors with the LogError method. These will prove to be useful for testing small bits of code without actually having to implement them, as you will see later on.

Advertisements