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.

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.