- Home /
Coroutoutine doesnt work properly.
I have this coroutine
IEnumerator Test(GameObject p)
{
print("s");
yield return new WaitForSeconds(0.4f);
Destroy(p);
print("S");
}
which is running properly till print("s"); but doesnt after yield return new WairForSeconds(0.4f);
Any ideas?
How do you start the Couroutine? Can you show that part of the code please?
Could it be that you are destroying the object that this script is attached to? I would still expect it to print "S" though. Is there any message in the console?
Answer by Bunny83 · Mar 07, 2017 at 11:32 AM
You either have:
destroyed the gameobject / script where the coroutine runs on
set your
Time.timeScale
to 0 in which case the WaitForSeconds will wait forever.or you call StopCoroutine / StopAllCoroutines somewhere on that script.
Answer by Denaton · Mar 10, 2017 at 08:57 PM
Make sure p is not Null. You can do a null check like this
if(p)
Destroy(p);
The Coroutoutine crashes do not return a error message, unless you use try{}catch(){} and log it your self. I think it is because its not rum in the MonoBehaviour class, but i might be wrong.
Answer by shadowpuppet · Mar 10, 2017 at 07:21 AM
yeah, how does the coroutine start? Try this
public float PrintSomething = .8f;//variable for coroutine delay
void Awake()
{
StartCoroutine(letsPrint());
}
private IEnumerator letsPrint(){
yield return new WaitForSeconds(PrintSomething );
print ("s");
}
}
He has to start it correctly or he wouldn't see the first print statement.
When he just does this:
Test();
nothing would happen at all. So he does use StartCoroutine.