Destroying the explosions after use

EDIT: I guess I should have looked ahead at the upcoming lectures, this was explained in lecture 155. :stuck_out_tongue:

As a self-assigned challenge I decided to clean up the explosions, and I thought I would share it in case someone else found it helpful.

First I tried messing around with coroutines, but that was way too complicated. Eventually I checked the API and found you could add a timer to the Destroy() method, so I just made a script called SelfDestruct and added it to the death effect prefab. After that I just put:

Destroy(gameObject, 2f);

in the Start method of the class.

I would be curious to know if anyone else has a better or cleaner way of doing it, or maybe you just want to share your own ideas.

Thanks,
Matt

I would say that your approach is clean and perfectly valid. If you didn’t want to create another script, you could also call destroy right after instantiate and pass the explosion game object with a delay as well.

1 Like

There’s no bug. You have to disable “Looping” in the particle system (second option, right after duration).

I just tested it on 2018.3.0b6 and it works. Since I had the same “bug” without disabling “looping”, I’m suspecting it would’ve helped in earlier versions as well…

Cheers,
Felix

1 Like

I am using Unity 2019.1.7.f1 and still had the same “bug” which I fixed by turning off “Looping” in the particle system and set the “Stop Action” to Destroy and it worked. I didn’t need an extra script to destroy the death particles. Also, I decided to turn “Looping” off for the hit particles as well. Both the death particles and the hit particles are one shot animations so it doesn’t make much sense too have them looping with no visible animation.

Privacy & Terms