How can we use coroutine inside a function?
Hello everyone, i'm facing a problem in my script, i want to use a coroutine inside this function : void OnCollisionEnter2D(Collision2D other) { source.clip = hitsound; source.Play ();
Die();
}
as i want my bird gameobject to make a hit sound when he collides with something, after 2 sec for example, he dies, i don't know how i make this, i know that Inumerator Coroutine() is a function, a function can't be inside another function as we have here, i'm confused.
Answer by hexagonius · Jun 21, 2017 at 05:07 AM
Make the return type of OnCollisionEnter2D an IEnumerator.
Now you can yield within it. Check the docs for which callbacks support this, but collisions do, Start...
Answer by Anass1997 · Jun 21, 2017 at 05:51 PM
That's it ,i used invoke function it works well. void OnCollisionEnter2D(Collision2D other) { source.clip = hitsound; source.Play ();
Invoke ("Die", 0.2f);
}
void Die()
{
Application.LoadLevel(Application.loadedLevel);
}