From ETC Public Wiki
Jump to: navigation, search

Debugging in Unity

Debugging within Unity is primarily done through the Debug.Log("text") function.

void Update()
     // Output text to the console
     float testFloat = 5.5f;
     Debug.Log("The current value of testFloat is " + testFloat + ".");

You can use string arithmetic to build strings containing whatever variables you might want to watch into your output. You can also use Debug.LogWarning and Debug.LogError to add the yellow triangle or red stop sign to your output. LogError in particular can be helpful because it can function like an artificial breakpoint when used with the Error Pause option on the Console. When an error happens and Error Pause is enabled, Unity will automatically pause execution and you can look at anything you want in the Editor vand the Inspector.

Debugging with MonoDevelop

Traditional breakpoint based debugging can be done with MonoDevelop, but is not as simple or reliable as it would be in other environments because of the way Unity handles script execution order. To use breakpoints to debug in MonoDevelop, go to Run and choose Attach to Process. File:AttachToProcess.png
Then select the Unity Editor process in the window that appears and click Attach. File:AttachToProcess2.png
Go back into Unity and hit Play. When Unity hits the breakpoint in your script, it will switch over to MonoDevelop and you can step over, into, through, and so on like you would in an IDE like Visual Studio. Be warned that debugging like this is sometimes unstable and Unity or MonoDevelop may crash, so be sure to save any work in both the Unity project, scene, and open scripts in MonoDevelop before debugging in this way.