Better selfdestruct on animation stopped?

Wouldn’t it be more clean and generalized to have selfdestruct.cs keep track of the explosion particle systems and if they stopped playing destroy them? This way the selfdestruct script can be put on the “Spawn at runtime” game object and it will keep itself clean no matter what particle systems are playing under it + for different animations with different end times:

    void Update()
    {
         foreach(ParticleSystem vfx in this.GetComponentsInChildren<ParticleSystem>()) {
            if(vfx.isStopped)
            {
                Destroy(vfx.gameObject);
            }
        }
    }

or is this an expensive operation to do as update() each frame?

Hi,

First of all, it’s great to see that you are trying to come up with your own solutions. That’s a great way to learn. :slight_smile:

Unfortunately, your current approach is fairly resource-intensive because for the following reasons:

  • GetComponentsInChildren is a relatively expensive method
  • calling GetComponentsInChildren each frame even though one could cache the returned array in a variable is a waste of resources

Since this is a rather small project, the difference between your solution and Rick’s is probably noticeable when it comes to performance. If you like your code, keep it.

And if you would like to explore another solution, here’s a new challenge for you: The ParticleSystem has got a performant built-in self-destroyer. Can you find it? If so, feel free to use it in your project and to remove the SelfDestroyer script.

Keep up the good work! :slight_smile:


See also:

i had almost the same Idea:
image

I think GetComponent certainly has the same Problem as GetComponentInChildren because of the Performance. Now I implemented your Idea Nina:


Thanks for the Advice :slight_smile:

Awesome, thanks for sharing your solution. Since it is not necessary to assign the desired stopAction each frame, you could rename the Update method to Start to increase the performance of your code.

Keep up the good work! :slight_smile:

ah right, thx for the answer :smiley:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms