Skip to content

Latest commit

 

History

History
74 lines (40 loc) · 2.26 KB

Unity-Scripting-Debugging.md

File metadata and controls

74 lines (40 loc) · 2.26 KB

Reference Sheet - Unity C# Scripting - Debugging

General

  • Always keep the console open

Debug.Log tips

  • Add a comma + gameObject name to be able to highlight the object in the scene Debug.Log(gameObject.name, gameObject);
  • Use Debug.LogWarning("string") or Debug.LogError("string") to improve your logs

Profiler and Debugging

Error handling

Editor Crashes

When the Unity Editor crashes or becomes unresponsive you will see no specific line numbers or other clues which tell you where problem lies.

Be careful with loops!

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;
}

Sources