Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
6
Question by GutoThomas · Jul 02, 2012 at 12:06 AM · coroutineyieldienumerator

What can cause a coroutine to not being working?

Sometimes my Coroutines just stop working. It's like if the yield command is just returning from the method and it stop his execution there. Here's my coroutine, it was working Ok like 2 hours ago. I had opened Unity sometimes already and nothing.

 using UnityEngine;
 using System.Collections;
 
 public class Transition : MonoBehaviour {
     
     public ParticleSystem desintegrationParticles;
     public Transform fadeOutTransform;
     public float velocity;
     
     void Start () {
     
         StartCoroutine (DesintegrateAndFade () );
         
     }
     
     IEnumerator DesintegrateAndFade () {
         
         Debug.Log("Coroutine started");
         yield return new WaitForSeconds(3);
         Debug.Log("Passed 3 seconds");
         desintegrationParticles.Play ();
         
         while(fadeOutTransform.position.z > -1) {
         fadeOutTransform.position -= Vector3.forward * Time.deltaTime * velocity;
         yield return null;
         }
         
         Application.LoadLevel("languageSelection");
         
     }
 }


I don't get the "Passed 3 seconds" debug, too. No errors, no crashes, it's just not passing from the yield command...

Someone can help me?

*Also, the coroutines aren't working in the whole project. Where I call the yield command the coroutine just stop his execution, or something like that.

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 GutoThomas · Jul 02, 2012 at 03:30 AM 0
Share

Ideas, hints, anything?!..

avatar image GC1983 · Jul 02, 2012 at 05:25 AM 0
Share

Sometimes the yield call will cause the function to cease. Try taking it out. If you really need the time yielded, id recommend creating a time.deltaTime counter.

avatar image asafsitner · Jul 02, 2012 at 05:28 AM 1
Share

I presume you get the "Coroutine started" debug message?

I've had a similar issue, actually, with WaitForEndOfFrame. No idea what caused it but switching everything to yield return null solved it. Perhaps it's a problem with the coroutine scheduler?

avatar image GutoThomas · Jul 02, 2012 at 06:13 AM 0
Share

GC83 - If I was starting with the project it would be O$$anonymous$$ to use a Time.deltaTime, but now, I'm too advanced on it to make such a core change.

asafsitner - I've had this problem more than once, but in the last times, just restarting Unity solved it. Now it's like permanent! It's funny because if I try with yield return null it works, but I really need the WaitForSeconds in some cases...

avatar image GC1983 · Jul 02, 2012 at 06:15 AM 0
Share

Its not that big of a change though. Create an int variable for the counter and add in an if statement.

Show more comments

9 Replies

· Add your reply
  • Sort: 
avatar image
17

Answer by Charles-Van-Norman · Mar 10, 2013 at 06:22 AM

FUTHERMORE: If you call a Coroutine inside object B from object A, then destroy object A, coroutine on object B hangs on yield function (even though object B was not destroyed.)

// THIS will BREAK if the object that calls this IEnumerator is destroyed: public IEnumerator RandomFunctionA(){ yield return new WaitForSeconds(1); // do stuff }

// SOLUTION: public void RandomFunctionA(){ StartCoroutine(RandomFunctionAcoroutine()) }

IEnumerator RandomFunctionACoroutine(){ yield return new WaitForSeconds(1); // do stuff }

Comment
Add comment · Show 6 · 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 monoRAIL · Dec 05, 2013 at 02:23 AM 1
Share

Thanks for this - I ran into this exact problem although my Object A wasn't being destroyed, it had SetActive (false) called on it. That's enough to stall a coroutine called from that object indefinitely.

avatar image canadialad · Sep 19, 2014 at 04:30 PM 0
Share

This is the perfect solution to my problem. Thank you!

avatar image Andres-Fernandez · Nov 06, 2014 at 03:48 PM 0
Share

$$anonymous$$an, I was about to jump out the window before I found this explanation. NEVER destroy (or even disable) the object that starts the coroutines. Thanks.

avatar image Karwoch · Jun 09, 2015 at 10:50 AM 0
Share

Yes, my hair was in a big danger because of this! Thank You!

avatar image oxysofts · Oct 04, 2016 at 04:15 PM 0
Share

Thanks a lot for this, you potentially saved me hours of debugging

Show more comments
avatar image
1

Answer by GutoThomas · Jul 02, 2012 at 06:26 AM

I just tried this:

IEnumerator Example () {

float counter = 0; float target = 5;

while(counter < target) { counter += Time.deltaTime; yield return null; }

}

And, fortunatelly it worked! I have some dozens of Coroutines and I'll probably will need to this this for each of them!

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 whydoidoit · Jul 02, 2012 at 07:19 AM 0
Share

That is categorically not how to do it! You have something else wrong.

avatar image GC1983 · Jul 02, 2012 at 09:30 AM 0
Share

told ya. =) yield isnt really the best to use to use in an IEnum, I think. It stops the function for a period of time unlike time.deltaTime which keeps the moving with the integer count. This especially accounts for co-routines.

avatar image whydoidoit · Jul 02, 2012 at 09:54 AM 0
Share

@GC83 - that's not right - every one of my coroutines does a yield WaitForSeconds and they work - if they don't then it's a bug - but that has never happened to me. Doesn't mean it isn;'t there though. If you can build a replicable example of this then please submit it to Unity Support (and post it here and I will submit it to Premium Support if you don't have it).

avatar image Andy-Block · Mar 18, 2013 at 09:54 AM 0
Share

A solution similar to this also seems to fix a similar problem we had.

avatar image
1

Answer by whydoidoit · Jul 02, 2012 at 07:18 AM

The only thing you can yield to stop a coroutine is yield break.

Coroutines will stop if the object they are connected to is destroyed. Calls to StopAllCoroutines in another script on the object could halt this one too. I imagine that this script is probably attached to an object that is being destroyed though.

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

Answer by stevecsalkers · Apr 17, 2016 at 05:58 PM

Yield return new WaitForSeconds(3f); It can be 3.0f as well. As far as I experienced waitforseconds works best and most reliably with floats.

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 GutoThomas · Jul 02, 2012 at 05:23 PM

Mike, I know the way I did the yield is not the best way to do it, but it worked, seems like just yield return null is working. But of course WaitForSeconds is a lot better to use and it's a lot more straight forward, too.

I have coroutines in a lot of objects and everything was working and stopped from one hour to another. The objects aren't being destroyed, so that's not what is causing the break of the coroutine.

I can create another script with the simplest coroutine ever and attach to any object (which will not be destroyed until the coroutine was supposed to end his execution ) and nothing happens. If I put a print after 2 seconds, this print is never debugged in the console, for example.

I created another project and pasted the assets on it and it worked, but now I need to set up everything again, which is one more headache.

Comment
Add comment · 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 whydoidoit · Jul 02, 2012 at 05:34 PM 0
Share

It's a serious bug if it is happening - would break a lot of my code immediately if it happens to me. Feeling worried now!

  • 1
  • 2
  • ›

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

18 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

Related Questions

Calling IEnumerator function with variables does not do anything 1 Answer

Waiting twice inside coroutine (C#) 2 Answers

Coroutines not passing yield 1 Answer

What am I doing wrong with IEnumerator? 1 Answer

[ Coroutine: Move Next ] CPU usage 0 Answers


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