Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
3
Question by $$anonymous$$ · Sep 28, 2017 at 04:09 PM · waitforsecondsenumerate

yield return new waitforseconds(5f) doesn't finish

I have written the following piece of code and the problem is that the WaitForSeconds Method doesn't exit / finishes and I don't know why.

 IEnumerator increaseDelay(){
     ground.disable();
     float oldDelay = spawn.getDelay ();
     spawn.setDelay (spawnRate);
     yield return new WaitForSeconds (5f);
     spawn.setDelay (oldDelay);
     ground.enable ();
 }
Comment
Add comment
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

5 Replies

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

Answer by Bunny83 · Sep 28, 2017 at 11:25 PM

The only reasons why a coroutine won't finish executing is:

  • You destroyed the gameobject or the MonoBehaviour which the coroutine runs on. The hosting MonoBehaviour is the one where you used StartCoroutine, not necessarily where the coroutine is located.

  • You deactivated the gameobject or disabled the hosting MonoBehaviour.

  • You called StopAllCoroutines or StopCoroutine manually which stops the execution.

Keep in mind that coroutines can only be stopped / aborted at "yield points" as those are the points where the control is passed back (yielded) to Unity. At this time you most likely performed any of the above mentioned steps which will terminate the coroutine at the current point.

Coroutines are not "methods" but are translated into statemachine objects. Each call of "MoveNext" on the IEnumerator object will advance the statemachine to the next yield. Unity will do that internally. It inspects the "Current" value of the statemachine and based on that value it decides when to resume this coroutine.

When the MonoBehaviour is destroyed the coroutines stored internally will simply be removed along.

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 $$anonymous$$ · Sep 29, 2017 at 10:41 AM 0
Share

First of all huge thanks for your long explanation :) And and I didn't know that the Coroutine stops after I had destroyed the Object. So thanks again and I think I can now fix this on my own :)

avatar image Mr_Master_Matt · Jul 20, 2018 at 01:33 PM 0
Share

Very helpful explanation. $$anonymous$$y problem was that I destroyed the gameobject hosting my coroutine.

avatar image Northe · Jul 23, 2020 at 02:53 PM 4
Share

OR you stopped time (Time.TimeScale = 0). In this case use WaitForSecondsRealtime.

avatar image
3

Answer by tulaib-pirzada · Oct 17, 2018 at 03:22 AM

Following can be reasons due to which your coroutine gets stuck on WaitForSeconds:

  1. The time given in WaitForSeconds is too long. Although this is silly reason, but sometimes this happens

  2. The object from which you are calling the coroutine is getting destroyed. You can check that as follow.

      void OnDestroy() {
             Debug.Log("Destroyed");
         }
    
  3. From any place, StopAllCoroutines() is getting called which is stopping your coroutine.

  4. Lastly, check that anywhere Time.timeScale is getting set to 0. Because when its zero, it stops Time.deltaTime from increasing and as a result WaitForSeconds never reaches your specified value.You can check your current timescale in Edit -> Project settings -> Time check Time Scale parameter in that. Or you check its value by logging before your coroutine is called.

Comment
Add comment · 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
0

Answer by FernandoGBR · Sep 28, 2017 at 04:18 PM

add

 yield return null;

at the end of the coroutine.

Comment
Add comment · Show 4 · 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 $$anonymous$$ · Sep 28, 2017 at 04:22 PM 0
Share

Unfortunately this hasn't changed a thing :(

avatar image FernandoGBR $$anonymous$$ · Sep 28, 2017 at 04:41 PM 0
Share

If I am not wrong coroutines wont execute after the last yield return statement. Please correct me if I am.

Are you changing the TimeScale in spawn.setDelay()?

Try to write something in console after calling your functions. I am nearly sure that if you are nomt changing the time scale there should be no problem with your new wait for secondscall

avatar image Bunny83 FernandoGBR · Sep 28, 2017 at 11:12 PM 1
Share

No, this is wrong. Coroutines (or generator methods in general) do execute the code after the last yield. This is the code that is executed when the coroutine actually finishes. Yielding once again at the end is pointless.

Show more comments
avatar image
0

Answer by mwmwmw · Dec 16, 2021 at 08:48 PM

I have run into this issue before, and it happens when I call my coroutine directly like a method.

It's an easy mistake to overlook.

The proper way to call a coroutine is: StartCoroutine(myFunction());

Comment
Add comment · 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
0

Answer by Durium · Mar 20 at 05:35 PM

Had to use "WaitForSecondsRealtime()" since i am altering the Time.timeScale = 0... If anyone has the same iossue trey this

Comment
Add comment · 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

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

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

check bool in WaitForSeconds 2 Answers

Coroutines with Yield return new waitforseconds(); go for execution of other functions? 0 Answers

bounce player back to position while jumping 0 Answers

Instantiate color change loop 0 Answers

run level for 4 minutes help 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