hi everyone, my problem is that i want to play a particle when my enemy dies, but the "destroy(GameObject)" wont let the particle play here the code Code (CSharp): void OnTriggerEnter2D(Collider2D col) { if (col.gameObject.tag == "Player") { float yOffset = 0.6f; if (transform.position.y + yOffset < col.transform.position.y) { yield return new WaitWhile(ParticleChan.Play()); Destroy(gameObject); } } } } EDIT: i was tryng to use any wait coroutine but it dont work xd
It's not possible to yield non enumerators; Do something like this instead: Code (CSharp): private bool _playingFX; void OnTriggerEnter2D(Collider2D col) { if (_playingFX) return; if (col.gameObject.tag == "Player") { float yOffset = 0.6f; if (transform.position.y + yOffset < col.transform.position.y) { _playingFX = true; StartCoroutine(FXCoroutine()); } } } private IEnumerator FXCoroutine(){ ParticleChan.Play(); while (ParticleChan.IsAlive()){ yield return null; } Destroy(gameObject); }
Ah, glad you got it working. I did think line 9 looked a bit funny, but wasn't certain what the results from it would be.
Add the particle to a empty gameobject Make prefab of your object then Public gameobject your prefabparticle; Then in the coroutine just instantiate it