Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //=====================================================================================
- // Author: Stephen Spanner
- // Class : ParticleSystemAutoDestroy
- // Desc : A helper class used to automatically remove gameobjects that have particle
- // systems. If no particle system is found, it will simply remove just the
- // script and keep the game-object intact.
- //=====================================================================================
- using UnityEngine;
- using System.Collections;
- public class ParticleSystemAutoDestroy : MonoBehaviour
- {
- private ParticleSystem mPSys;
- // --------------------------------------------------------------
- // Start()
- // Check to see if a particle system exists, and possibly remove
- // the script if none
- // --------------------------------------------------------------
- public void Start()
- {
- // Grab the particle system of the component, can only be on particle system object
- // and NOT the child.
- mPSys = GetComponent<ParticleSystem>();
- // Check particle system exists
- if( mPSys == null )
- {
- Debug.LogWarning( "[ParticleSystemAutoDestroy] WARNING: No Particle System found on " + gameObject.name );
- Destroy( this );
- }
- }
- // --------------------------------------------------------------
- // Update()
- // Every frame, check to see if Particle System has expired, and
- // then destroy the gameobject if necessary.
- // --------------------------------------------------------------
- public void Update()
- {
- if( mPSys )
- {
- // Check to see if particle system has expired its lifetime, if so, destroy it.
- if( !mPSys.IsAlive() )
- {
- Destroy( gameObject );
- }
- }
- else
- {
- // Particle System was removed somehow, destroy this script.
- Debug.LogWarning( "[ParticleSystemAutoDestroy] WARNING: Particle System was removed from " + gameObject.name );
- Destroy( this );
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement