Advertisement
Guest User

ParticleSystemAutoDestroy

a guest
Jan 11th, 2017
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.89 KB | None | 0 0
  1. //=====================================================================================
  2. // Author: Stephen Spanner
  3. // Class : ParticleSystemAutoDestroy
  4. // Desc  : A helper class used to automatically remove gameobjects that have particle
  5. //         systems. If no particle system is found, it will simply remove just the
  6. //         script and keep the game-object intact.
  7. //=====================================================================================
  8.  
  9. using UnityEngine;
  10. using System.Collections;
  11.  
  12. public class ParticleSystemAutoDestroy : MonoBehaviour
  13. {
  14.     private ParticleSystem mPSys;
  15.  
  16.     // --------------------------------------------------------------
  17.     // Start()
  18.     // Check to see if a particle system exists, and possibly remove
  19.     // the script if none
  20.     // --------------------------------------------------------------
  21.     public void Start()
  22.     {
  23.         // Grab the particle system of the component, can only be on particle system object
  24.         // and NOT the child.
  25.         mPSys = GetComponent<ParticleSystem>();
  26.  
  27.         // Check particle system exists
  28.         if( mPSys == null )
  29.         {
  30.             Debug.LogWarning( "[ParticleSystemAutoDestroy] WARNING: No Particle System found on " + gameObject.name );
  31.             Destroy( this );
  32.         }
  33.     }
  34.  
  35.  
  36.     // --------------------------------------------------------------
  37.     // Update()
  38.     // Every frame, check to see if Particle System has expired, and
  39.     // then destroy the gameobject if necessary.
  40.     // --------------------------------------------------------------
  41.     public void Update()
  42.     {
  43.         if( mPSys )
  44.         {
  45.             // Check to see if particle system has expired its lifetime, if so, destroy it.
  46.             if( !mPSys.IsAlive() )
  47.             {
  48.                 Destroy( gameObject );
  49.             }
  50.         }
  51.         else
  52.         {
  53.             // Particle System was removed somehow, destroy this script.
  54.             Debug.LogWarning( "[ParticleSystemAutoDestroy] WARNING: Particle System was removed from " + gameObject.name );
  55.             Destroy( this );
  56.         }
  57.     }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement