Unity – How to stop Play Mode in case of infinite loop?

I just got into this situation: accidental infinite loop, stuck in play mode on a scene with unsaved work, Unity unresponsive.

But I was lucky enough to have Monodevelop open, even though I was mainly using Sublime for scripting. I pressed the button to start debugging near the top left of the Monodevelop window, then hit the pause button. Execution paused on a line in the middle of my infinite loop. Windows task manager confirmed Unity was no longer locking the CPU.

In Monodevelop, I was then able to find an object obj that the next line would attempt a method call on, and use the “Immediate” window to execute obj = null. Then unpause. Unity itself now unlocks because of the null pointer error, and I could take it out of play mode and save my work.

(Unity 2017.4.1f1 Personal, Windows 10 Home x64, Monodevelop 5.9.6)

Note, I got the idea from reading @Kinxil’s answer, but I had to take a slightly different approach because there was no “blocking value”. I had a for (;;) loop that had previously been inside a coroutine and I changed it to be inside a FixedUpdate() without removing the loop. :\ So causing an exception was the only option I could think of.

Leave a Comment

tech