- Home /
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.
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.
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?
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...
Its not that big of a change though. Create an int variable for the counter and add in an if statement.
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 }
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.
This is the perfect solution to my problem. Thank you!
$$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.
Yes, my hair was in a big danger because of this! Thank You!
Thanks a lot for this, you potentially saved me hours of debugging
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!
That is categorically not how to do it! You have something else wrong.
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.
@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).
A solution similar to this also seems to fix a similar problem we had.
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.
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.
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.
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!
Your answer
Follow this Question
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