Neater way to destroy explosion gameObject

I didn’t really like the configurable 1 second timeout before the particle gameobject was destroyed. Reason for this is that when you would (for some reason) increase the lifetime of the efect to something larger you would have to remember to change the 1 second.

A (I believe) neater solution would be to use the actual max lifetime of the effect. This can be found using:
explosion.GetComponent<ParticleSystem>().main.startLifetime.constantMax

2 Likes

In Unity Version 2017.2, the particle system got a new parameter Stop Action on the “Main” module. The default value is “None”. When you set it to “Destroy”, the game object with the particle system will be destroyed as soon as there are no alive particles anymore. It should be noted that this option should only be used when “Looping” is turned off.

3 Likes

Do you plan on Instantiating explosion objects frequently or will there only ever be a single explosion at a time?

If you plan on having a number of them then you might want to look into object pooling which would allow you to generate ‘x’ number of explosions at the start and just turn them on when you need. Once done you’d just return it to the pool to be used again later.

If you’re interested in taking that route there’s loads of resources out there I could point you in the direction of.

Cheers,
Todd

1 Like

Hi SelfAvenger,

That does sound like a efficient way of handling resources. I’m very interested if you have a favourite reference for this.

Cheers

Hi @Tauq,

Apologies for my late reply. Brackeys has a great simple to follow tutorial so you should definitely check out their channe if you haven’t already. Link to the specific object pooling tutorial is below:

All the best with it.

Cheers,
Todd

2 Likes

This advanced approach (Object Pooling) is extremely helpful for managing processing power.

Thanks @SelfAvenger for mention this.

1 Like

Thank you very much for this useful link! :pray:

Privacy & Terms