Search Unity

help, particle play on dead

Discussion in 'Scripting' started by JhonnyRage, Mar 15, 2019.

  1. JhonnyRage

    JhonnyRage

    Joined:
    Feb 23, 2017
    Posts:
    57
    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):
    1.     void OnTriggerEnter2D(Collider2D col)
    2.     {
    3.         if (col.gameObject.tag == "Player")
    4.         {
    5.             float yOffset = 0.6f;
    6.          
    7.             if (transform.position.y + yOffset < col.transform.position.y)
    8.             {
    9.                 yield return new WaitWhile(ParticleChan.Play());
    10.                 Destroy(gameObject);
    11.             }
    12.         }
    13.     }
    14. }
    15.  
    EDIT: i was tryng to use any wait coroutine but it dont work xd
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    Try adding a delay to your destroy and see if that works.
     
    JhonnyRage likes this.
  3. JhonnyRage

    JhonnyRage

    Joined:
    Feb 23, 2017
    Posts:
    57
    yep i already try that but i dont manage to make the coroutines work
     
  4. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    It's not possible to yield non enumerators;
    Do something like this instead:
    Code (CSharp):
    1. private bool _playingFX;
    2.  
    3. void OnTriggerEnter2D(Collider2D col)
    4.     {
    5.         if (_playingFX) return;
    6.         if (col.gameObject.tag == "Player")
    7.         {
    8.             float yOffset = 0.6f;
    9.        
    10.             if (transform.position.y + yOffset < col.transform.position.y)
    11.             {
    12.                _playingFX = true;
    13.                StartCoroutine(FXCoroutine());
    14.             }
    15.         }
    16.     }
    17.  
    18. private IEnumerator FXCoroutine(){
    19.      ParticleChan.Play();
    20.      while (ParticleChan.IsAlive()){
    21.             yield return null;
    22.      }
    23.  
    24.      Destroy(gameObject);
    25. }
    26.  
     
    JhonnyRage likes this.
  5. JhonnyRage

    JhonnyRage

    Joined:
    Feb 23, 2017
    Posts:
    57
    wow yeah, ty that probably work n_n
     
    Last edited: Mar 15, 2019
  6. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    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.
     
    JhonnyRage likes this.
  7. JhonnyRage

    JhonnyRage

    Joined:
    Feb 23, 2017
    Posts:
    57
    problem, the scrip looks like it should work, but... when i jump over a enemy it wont do anything...
     
  8. j04milan

    j04milan

    Joined:
    Feb 6, 2019
    Posts:
    44
    Add the particle to a empty gameobject Make prefab of your object then

    Public gameobject your prefabparticle;

    Then in the coroutine just instantiate it
     
    JhonnyRage likes this.
  9. JhonnyRage

    JhonnyRage

    Joined:
    Feb 23, 2017
    Posts:
    57
    yep ty i resloved it already :p