- Always keep the console open
- Add a comma + gameObject name to be able to highlight the object in the scene
Debug.Log(gameObject.name, gameObject);
- Use
Debug.LogWarning("string")
orDebug.LogError("string")
to improve your logs
- Unity Profiler
- View while in editor OR
- Profile your application by attaching the Profiler to a build (check
Development Build
,Autoconnect Profiler
,Deep Profiler Support
, andScript Debugging
in build settings)
- Unity Log file locations
When the Unity Editor crashes or becomes unresponsive you will see no specific line numbers or other clues which tell you where problem lies.
Runaway loops will always cause the editor to become unresponsive. This example uses a safety
var to prevent runaway loops
// example function with a logic error and "safety"
Vector3 ReturnNewFloatWithError ()
{
float newFloat = -1; // default is -1 so loop starts
int safety = 0; // a var to keep track of how many loops have occurred
// loop until condition is false, if expression is always true then the loop will never stop
while (newFloat <= 0) {
// create new float (this is the logic error, it should be creating a positive value so the loop stops)
newFloat = Random.Range (-1,0); // this is the logic error
// this is the safety, which realistically should never occur, but can help you find other errors in development
if (++safety > 10) {
Debug.Log ("ReturnNewFloatWithError() - Safety first!");
break;
}
}
return newFloat;
}
- Unity Scripting Reference
- Chapter 8 in Halpern, Jared. Developing 2D Games with Unity. APress, 2019.