Is there a way to brute-force interrupt a game without closing Unity itself in case of, say … an infinite loop?
Or is there another way to deal with infinite loops?
I can’t remember reading anything about that in the manual and couldn’t find a forum topic about it.
the best way is to not allow them.
Add a sentinel ie a variable that starts out at the max loop count and is decreased by 1 each run. add variable >= 0 as loop condition and you won’t have an infinite loop anymore.
Unity also has uncatchable exceptions that can happen in a MonoBehaviour event. When those happen, the event exits and is recalled and if the same exception happens again you can get stuck in an infite loop.
The best way to find the problem is leave the editor log open. If the UI stops responding and keeps logging, you’ve entered an infinite loop.
Unfortunately, Unity has no provision for stopping a runaway loop. It just has to be killed. My general rule is to be very careful to not make an infinite loop, I’ve had them bite me before. However, some infinite loops aren’t so obvious, why they’re called bugs.
The best thing to do is to post this on the Wish List. Again.
Also, it might be possible to retake control of the engine through some other means. It could possibly be done through a script, but making it fool proof would probably get too messy to make it practical. I’m thinking that attaching a debugger and forcing an end to the simulation would work best. Debugger, another for the list. Again.
I’m finding it a little absurd that you can’t even interrupt the execution with the play/stop button. Every IDE I’ve ever seen handles this properly, and yet Unity, theoretically user friendly, falls apart.
Certainly we should try to avoid infinite loops, but programming oversights happen… and in every other IDE under the sun, MSVC for instance, you realize the mistake, hit “Stop Debugging”, and you’re golden. Here? Not possible, apparently.
If the Unity devs happen to read this - shame on you. I do realize the integrated nature of the Unity editor makes it more difficult to keep the control logic in a separate thread, but the idea of an IDE that you have to ctrl-alt-del any time you make a coding mistake (and an IDE that doesn’t auto-save on execution no less, meaning you often lose work) is somewhat ridiculous.
It does auto-save whenever you hit play, but not in a particularly obvious way. As long as you haven’t run Unity again after a force-quit, the scene backup is in the Temp folder.
I was just able to stop an infinite loop. I attached the visual studio debugger, put a break point inside the loop. then while paused at that break point I could change the value in the stop condition of my loop.
while(transform.childCount < maxChildren)
{
//accidentially adding children to the wrong transform causing infinite loop.
}
I was able to change maxChildren to -1 using visual studio’s debugger breaking the loop.