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.
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
}
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.
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.
I just had something very similar to this problem. As posted above, things stop working if the object is destroyed. However, importantly, things also stop working if the object is disabled as well.
…elsewhere, in the depths of my code, I was disabling the game object this script was attached to. After sorting out that bit of logic to only disable it when the work was all done, everything started working properly again.
This seems to be a bug that can sometimes occur when using WaitForEndOfFrame().
Like @asafsitner, I encountered this issue, and the cause was not destroyed/disabled objects. Even when the object running my coroutine was not destroyed and was enabled, the problem persisted when using
The game object existed and was enabled and active, and everything except “here3” printed out.
I’ve used Unity Coroutines for years, but I rarely ever use WaitForEndOfFrame. This is the first bug where a coroutine failed like this I’ve ever encountered; so my advice is, avoid using WaitForEndOfFrame.
I’m now getting this in Unity 4.
After upgrading to Unity 4 my coroutine worked fine for about a week, then I changed around an enum (completely separate from the coroutine) and the coroutine now mysteriously fails/stops working at a yield return new WaitForSeconds(3f);