- Home /
Call Method before OnDestroy()
When an object is destroyed, I Instantiate a particle system. I also destroy the object when they leave the screen. This causes the particle system to trigger when they leave the scene, which I don't want to happen, because the particles leak onto the screen.
I want the object play the particle system, but only if the object is Destroyed while it is on the screen. I have the following code in the OnDestroy() method:
ParticleSystem particleSystem = GameObject.Instantiate(MyParticleSystem, transform.position, Quaternion.identity) as ParticleSystem;
particleSystem.Play();
Destroy(particleSystem.gameObject, particleSystem.duration);
How can I tell it to trigger only if it is on screen? If I use particleSystem.renderer.isVisible, then it never appears, even if it is on screen.
One solution I can think of is to call the particle system before the OnDestroy() method, but I do not know how to do that.
Answer by Rebaken-Enterprises · Nov 17, 2014 at 12:36 AM
What about creating a new method "Die()". When your object goes off screen or is killed, call "Die()" instead of Destroy. You could pass a parameter about why they died Die( bool offscreen = false ) Then decide if you need to create the particle system or not and call Destroy.
Your answer
![](https://koobas.hobune.stream/wayback/20220613171712im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
OnDestroy() object with tag 1 Answer
Draw Particles over Text (GUIText or GUI.Label) 3 Answers
how to add a script ? 1 Answer