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.