DestroyAfterTime.cs 791 B

1234567891011121314151617181920212223242526272829
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Networking;
  5. /// <summary>
  6. /// Destroys the object it's attached to after a pre-specified amount of time. Used for explosion effects.
  7. /// Similar to Unity's Destroy(GameObject, float) overload, but allows it to be set easily in the Inspector.
  8. /// </summary>
  9. public class DestroyAfterTime : MonoBehaviour
  10. {
  11. /// <summary>
  12. /// How long the gameobject exists.
  13. /// </summary>
  14. [Tooltip("How long the gameobject exists.")]
  15. public float DeathClock = 2f;
  16. // Update is called once per frame
  17. void Update ()
  18. {
  19. DeathClock -= Time.deltaTime;
  20. if(DeathClock <= 0f)
  21. {
  22. Destroy(gameObject);
  23. }
  24. }
  25. }