Guide:Unity 3D debugging

From Game Making Tools Wiki
Revision as of 15:15, 16 October 2016 by Rjt (talk | contribs) (Moved from Guide:Unity_3D page, added more general explanation to the start)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

If you've followed along with the oficial tutorials, and you've never programmed before, one of the first problems you may come across is how to find out what is going wrong and how to fix it. Unfortunately they don't teach you anything about debugging D:

Error Messages

In the Unity editor there is a console, which will display error messages when problem are occured. These are not always game breaking, so sometimes the game may continue to run, but somtimes they may stop it from starting. By default this is not visible, you will only see single-line error messages in the status bar at the bottom of the editor window. Having the console visable will give you multiple lines to read.

An important part of these messages will be a pair of numbers in brackets (6,10), which tells you where you can find the problem! The first number is the line number (read from top to bottom), the second is the character number along that line. So (6,10) would be line number 6, 10th character along.

You can also write messages to this console yourself. This can help you check that things you want to happenning, even if there are otherwise no obvious signs. For example it may seem like your character is not taking damge so you can write a check to tell you when they're hit, or you can write values from variable to give you other info. Very handy!

Using the console

Debug.Log("This gam is v good");
Debug.Log(transform.position.y); //would tell you where the object the script is attached to is on the Y axis
var player : Transform;

function Update(){
    Debug.Log("Player's Y position is " + player.position.y); //where 'player' is a variable that is editable in the inspector
    }

Using the command 'Debug.Log' will write text to the console. This will appear when testing your game within Unity in the little status bar down the bottom of the screen, and in the console window (Window>Console / Ctrl+Shift+C). It's most useful when you use it to write text at different stages within your scripts to check if that step is reached, and to write variables to check what they're up to. Very useful when you get stuck scripting!

Displaying in-game

Apart from / as well as utilising the console, you can also use Unity's UI stuff to display similar information which will also appear when you do a proper build of your game.

To show the contents of a variable on screen you can use:

import UnityEngine.UI;

function OnGUI() {
	// Show timer
	GUI.Label(Rect(0,0,100,100), Time.time.ToString());
}

In this case it displays a timer, which starts when the game does. Time.time is handled by Unity automatically, and can't be modified.

You can use any other variable, just make sure you append .ToString() — this converts the value to a text string, which is the only way it can be displyed with this method.

You can mess with the values in the brackets after 'Rect', as they currently are they'll show the value in the top left of the screen.