- Home /
Can the Unity editor break out of an infinite loop in a script?
Hi, when I put an infinite loop in a script (on purpose, of course:) ), the whole Unity editor hangs when I press 'play'. Is there a way to break out of this? It seems a bit harsh to have to end the editor process and imho also contrary to the idea of scripting?
The problem is that the unity editor uses the same thread as the main game loop. It makes things a lot easier for editor scripting though, no need to add locks everywhere in your code to get around multithreading issues
Thanks, $$anonymous$$ike. Just to be clear: the deliberate infinite loop was a test after accidentally coding an infinite loop in a script before and hanging the whole thing. Oh, and losing my unsaved scene data...
This is a real problem, I have forgotten to increment a loop counter or (more commonly) yield from a tight loop in a coroutine a LOT and having to force quit the editor is a real pain in the neck, losing unsaved scene data for example.
Of course he isn't coding an infinite loop on purpose (testing aside)!
Some static analysis might be able to catch the usual suspects or at least warn about suspect code.
Note that similar behavior occurs if your script blocks for any reason. Examples include uses of WaitHandle or network calls with no (or too-long) timeouts.
Is there ever a good reason for having unsaved work?
That's a genuine question. I'm racking my brain trying to think of a reasonable excuse for it, and failing to come up with one. Obviously one can just forget, but the number of people talking about losing unsaved work on here is making me wonder if I'm missing something.
I$$anonymous$$HO not using some form of version control is bordering on insane. Not saving your work before pressing "Play" takes a step over that border.
To be fair, Unity still doesn't have autosave built in. If you need to press save before every Play, I'd say the IDE is at fault.
But bla$$anonymous$$g is boring of course. Install/make an Autosave editor extension. They're trivial to implement.
Yes, totally agree about the ID$$anonymous$$ Sushi271 made the same point in a comment below... as I said there, you'd think that at the very least, adding a "you have unsaved changes" warning to the play button would be straightforward enough.
Have to confess I'm probably a bit old school with this kind of thing, having started coding back in the day when nothing was ever done for you. It's true that there's no reason things should be that way in these days of IDEs and so on (although I think that taking full responsibility for everything yourself is still the safest attitude to take).
Answer by JaredThomson · Jan 11, 2014 at 05:20 PM
I know it's an old question but I do have a better solution.
As others have said: don't use an infinite loop by design. However if you did it by mistake and don't want to loose a whole scene or other unsaved data here's what you can do:
1) Open mono develop and attach it to the editor if you haven't already.(Run > Attach To Process)
2) Pause execution from mono develop.(Run > Pause)
3) Go to your immediate window (View > Debug Windows, if you don't see it at the bottom).
4) Modify the value that's causing you to loop infinitely. The Immediate window lets you just type in some code that will execute in that scope.
For me, the problem was looping over a list and trying to find a specific value that matched an element. I didn't properly handle the value not existing so it looped infinitely. All I had to do was call listname.Clear(); in the immediate window and the loop broke out on the next iteration.
If you were doing something exceptionally silly like while(true){ / some silly code / } you might be able to break out by just calling return. If you were calling that function every frame you might have to be a bit more sneaky and call T.Destroy(gameObject.GetComponent()) or something to that effect to remove the script that's calling that silly function.
I hope this helps someone else who doesn't want to nuke their work in progress!
Cheers.
Awesome, thanks a lot. I just tried this, and it works! Typing 'return' didn't work, but changing a variable value got me out of the loop safely. I normally work with UnityVS, and you cannot connect to Unity when it's 'hanging. Good old $$anonymous$$onoDevelop...
The immediate window is completely useless in my $$anonymous$$onoDevelop. Not surprising since $$anonymous$$D has a lots of bugs.
I tried this with Unity 5 and OS-X 10.10.2. Here's the good news: $$anonymous$$onoDevelop hangs when you open the AttachToProcess window.
Awesome. Thanks a million for that!
Stupid me had typed: while (ViewCamCanvas.alpha <5f) { ViewCamCanvas.alpha += 0.1f * Time.deltaTime; }
which of course was never going to happen as the alpha can't get over 1!...
Calling:
ViewCamCanvas = null;
in the Immediate window worked for me :)
Answer by duck · Nov 04, 2010 at 12:51 PM
Short answer - unfortunately, no - if you have already got Unity frozen in an infinite loop, your only option is to force-quit.
However, this question makes me wonder why you're putting an infinite loop in there on purpose in the first place. What are you using it for?
In general, in Unity, you would use the Update() and FixedUpdate() functions for "game loop" type code, because these functions get called every frame, and every physics step respectively, whilst releasing control to Unity's other systems (rendering, physics, etc) between steps so as to not lock up the entire thread. Eg, to some action every frame:
function Update() {
// increment a variable 'i' by 1 each frame:
i++;
}
Another method of creating a non-hanging loop is to use a Coroutine, which is a type of function which allows you to explicitly specify certain points where the execution should be paused and control released to other processes until a certain time.
An example of a "non hanging" loop in a coroutine, in Unity's Javascript:
function MyLoop() {
while (true) {
print("hello world!);
yield;
}
}
And in C#:
IEnumerator MyLoop () {
while (true) {
Debug.Log("hello world!);
yield return null;
}
}
Hope this is helpful.
Ah, too bad. Thanks for the reply. Well, the situation was that I had a while loop and I forgot to increment the loop variable. Silly, yes, but it happens to me sometimes...
Oooh, very helpful. Coroutines in Unity look pretty easy to implement too.
how about creating an infinite loop by mistake? I have to force quit and my later work is lost.
Personally I don't think it's right to blame the infinite loop for loss of work, that is caused by not saving your work.
Personally I don't think it's right to allow developer to run application, which isn't previously saved. Look at VS. Building project automatically saves it. Why isn't that the case with Unity?
Answer by Stephanie-The-Viking · May 14, 2015 at 05:04 PM
Apologies for the slightly off-topic response. However, when people get into an infinite loop and think they've lost their scene and come looking for help on this thread, this will be most useful to them:
without starting unity again. This is important, DO NOT start unity again Go into your game folder (in myDocuments usually I guess) Open the Temp folder in the game folder and look for a file called " __EditModeScene " Rename this file to the name of your scene in unity that was running when it crashed, so myScene.unity or whatever. Take this file and paste it into the Assets folder in your game folder, overwriting(or preferably backing up first) your scene of the same name. Open unity again and your scene will be exactly as it was when you pressed Play and crashed the program.
You're welcome.
Upgrade note: the backups are named __Backupscenes/backup.0, etc. in Unity 5.6, probably renamed to support multi-scene editing.
Answer by Will-H · Aug 27, 2015 at 09:02 PM
Hi @MvD !
Yes, it's possible with a plugin that I created which breaks infinite loops through a single shortcut.
With it, just press Shift+Esc and the editor is responsive again in a flash.
You can watch it in action here: https://youtu.be/USquReimPGE
I'm offering it on the Asset Store, it's called Panic Button :) http://u3d.as/hpe
Thanks! :) I think it can help streamline this painful part of the development process in Unity.
Sounds awesome. Great Niche ;) And you offer full source code. I hope you make plenty of dough from this.
Thanks a lot :D I hope it will help people. It's the result of about 2/3 intense months of brain-twisting on how to solve this problem.
Answer by boymeetsrobot · Nov 04, 2010 at 11:01 AM
I think it would be better to avoid an infinite loop as it is likely to make most IDE's hang (at least temporarily).
Visual Studio never hangs when working with C# or C++. O$$anonymous$$, not exactly the same situation, but still... It's a bit awkward that a script can hang the whole editor. I lost unsaved scene data as well...
It happens to me quite a bit - I am increasingly paranoid about it and save constantly, but there's always that time I made a shed load of changes and forgot!
This should not hang, this is undesirable behaviour in unity, not a feature and therefore a bug. You should be able to hit the play button again to hault your program, but you can't do this when a script hits an inf loop.
Your answer
Follow this Question
Related Questions
Quick For Loop Problem 2 Answers
Ping Pong Ball break infinite loop 3 Answers
Running multiple coroutines in loops with different time 2 Answers
Is this loop infinite? 2 Answers
Re-using the "same" for loop? 1 Answer