• 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
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
tylerwongj
felixmann
Lightning_A

People who like this

3 Show 0
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
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
MacDx
Mr_Master_Matt
tylerwongj
anshuman2967
felixmann
Lightning_A
Tactical_Programmer
D00MX
Steedie
caneva20
wcspracklin
MrWy07
jake_vr
HoldTheLine
ManoKristos
And 1 more...

People who like this

16 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 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. My 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 HoldTheLine Northe · Sep 06, 2022 at 01:06 AM 0
Share

You the real MVP. Thanks for adding to Bunny83's list. This was a hard lesson to learn.

avatar image

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
chris_gong
TheSmokingGnu
olejuer

People who like this

3 Show 1 · 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 Actually_Rico · Nov 18, 2022 at 09:22 PM 0
Share

Had a same problem and forgot about Time.deltaTime... Thank you so much. You just solved one of my problems :) Thank you

avatar image

Answer by Durium · Mar 20, 2022 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
HoldTheLine
MoHortus

People who like this

2 Show 0 · 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

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

add

 yield return null;

at the end of the coroutine.

Comment

People who like this

0 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

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

People who like this

0 Show 0 · 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

79 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 avatar image avatar image

Related Questions

yield in C# doesn't work, not event the sample code? 1 Answer

Wait For Seconds to Load level C# 2 Answers

is there any function call will be call after i activate an object ? 1 Answer

Invoke/yield in non MonoBehaviour classes 1 Answer

Why does Yield WaitForSeconds () only run once 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