Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
31
Question by MvD · Nov 04, 2010 at 10:07 AM · editorloopyieldinfinite

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?

Comment
Add comment · Show 7
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Mike 3 · Nov 04, 2010 at 01:08 PM 2
Share

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

avatar image MvD · Nov 04, 2010 at 01:16 PM 0
Share

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...

avatar image Bovine · Dec 31, 2012 at 01:04 PM 0
Share

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.

avatar image fish.soluble · Mar 20, 2015 at 09:05 PM 0
Share

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.

avatar image Bonfire-Boy · Oct 19, 2016 at 01:41 PM 0
Share

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.

avatar image Waz Bonfire-Boy · Oct 19, 2016 at 10:04 PM 0
Share

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.

avatar image Bonfire-Boy Waz · Oct 19, 2016 at 10:15 PM 0
Share

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).

13 Replies

· Add your reply
  • Sort: 
avatar image
42
Best Answer

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.

Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MvD · Jan 13, 2014 at 09:57 AM 0
Share

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...

avatar image ProjectSHiNKiROU · Feb 18, 2014 at 04:26 PM 1
Share

The immediate window is completely useless in my $$anonymous$$onoDevelop. Not surprising since $$anonymous$$D has a lots of bugs.

avatar image eelstork · Mar 14, 2015 at 10:27 AM 0
Share

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.

avatar image MvD · Mar 16, 2015 at 09:42 AM 0
Share

Rats! I have had no such problems, but I'm on Windows 7.

avatar image baroquedub · Mar 16, 2016 at 11:56 PM 0
Share

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 :)

Show more comments
avatar image
12

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.

Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MvD · Nov 04, 2010 at 01:12 PM 2
Share

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...

avatar image MvD · Jan 13, 2014 at 09:58 AM 0
Share

So it is possible, see below.

avatar image koboldskeep · Mar 10, 2014 at 09:38 PM 0
Share

Oooh, very helpful. Coroutines in Unity look pretty easy to implement too.

avatar image klolololol · Mar 23, 2015 at 03:22 PM 1
Share

how about creating an infinite loop by mistake? I have to force quit and my later work is lost.

avatar image Bonfire-Boy klolololol · Oct 19, 2016 at 01:35 PM 0
Share

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.

avatar image Sushi271 Bonfire-Boy · Oct 19, 2016 at 01:58 PM 0
Share

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?

Show more comments
avatar image
8

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image eduardoalvara2 · Mar 17, 2017 at 12:35 PM 0
Share

Wow I will test it!!

Thank you!

avatar image huulong · Apr 27, 2017 at 04:57 PM 1
Share

Upgrade note: the backups are named __Backupscenes/backup.0, etc. in Unity 5.6, probably renamed to support multi-scene editing.

avatar image
1

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

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MvD · Aug 28, 2015 at 11:35 AM 1
Share

Wow, excellent! If only Unity had been this bright...

avatar image Will-H MvD · Oct 06, 2015 at 09:52 PM 0
Share

Thanks! :) I think it can help streamline this painful part of the development process in Unity.

avatar image meat5000 ♦ · Oct 05, 2015 at 01:27 PM 1
Share

Sounds awesome. Great Niche ;) And you offer full source code. I hope you make plenty of dough from this.

avatar image Will-H meat5000 ♦ · Oct 06, 2015 at 09:59 PM 0
Share

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.

avatar image shieldgenerator7 · May 17, 2019 at 08:12 AM 0
Share

Hey when will this be updated for Unity 2019?

avatar image
0

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).

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MvD · Nov 04, 2010 at 01:13 PM 1
Share

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...

avatar image Bovine · Jan 29, 2013 at 08:20 PM 0
Share

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!

avatar image LeetRooster · Aug 11, 2013 at 03:06 AM 0
Share

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.

  • 1
  • 2
  • 3
  • ›

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

26 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

A little help with loops and understanding how to format them (C#) 3 Answers

Yield Not Working - While Loop 3 Answers

The AnimationClip must be marked as Legacy and loop 1 Answer

What's wrong with this code and is there an easier way to write it!? 1 Answer

While loop yield lag? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges