Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Particle System's particles destroyed when changing parent.

Discussion in 'Scripting' started by Epic-Username, Oct 9, 2016.

  1. Epic-Username

    Epic-Username

    Joined:
    May 24, 2015
    Posts:
    339
    I want a particle system to be removed from it's parent when destroyed so far i have created the following script:
    Code (CSharp):
    1. void OnDestroy(){
    2.         RemovePSystem ();
    3.     }
    4.  
    5.     void RemovePSystem(){
    6.         pSystem.transform.parent = null;
    7.         pSystem.transform.localScale = new Vector3 (1, 1, 1);
    8.         pSystem.Stop ();
    9.         Destroy (pSystem.gameObject,pSystem.startLifetime);
    10.     }
    But for some reason the particles on the screen disappear when this is called in OnDestroy(), But when i call this function in Start() the particles remain on the screen. Is it possible to keep the current particles on screen when calling this function from OnDestroy()?
     
  2. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    When you call this function from Start, *this* object is still around startLifetime seconds later, to invoke the Destroy command. However, if your calling this code from OnDestroy. *this* object is about to be removed. Thus startLifetimeSeonds from now, this instance of the object isn't around to Invoke the Destroy command. I'm not sure how Unity handles the Delayed Destroy, but it seems like that is the issue your having.

    Try creating a publicly exposed function on the GameObject that actually has the pSystem, and have it call Destroy(this,pyStem.startLifetime), and see if it works from the OnDestroy of the original object, since the pSystem's GameObject will still be around after its former parent is gone.
     
  3. Epic-Username

    Epic-Username

    Joined:
    May 24, 2015
    Posts:
    339
    The Problem isn't the particle system's gameobject getting destroyed that works fine how it is, when i remove "pSystem.Stop();" the particle system continues to emit new particles until it is destroyed. My problem is that the current particles disappear as soon as i remove the parent from OnDestroy(), but the particle system's gameobject remains until 0.4 seconds later which is pSystem.startLifetime.
     
  4. Zullar

    Zullar

    Joined:
    May 21, 2013
    Posts:
    650
    I also have this bug. When my wooden crate is destroyed I remove the particle system and emit particles... but any existing particles are wiped out.

    As a work-around I detach the particle system before I call Destroy().
     
    Menion-Leah likes this.
  5. Menion-Leah

    Menion-Leah

    Joined:
    Nov 5, 2014
    Posts:
    189
    I can confirm this (Unity 2018.4.12f1): if you change the parent of a particle system in its current parent's OnDestroy, all existing particles are removed immediately
     
    MiguelLobo likes this.