/// --------------------------------------------- /// Ultimate Character Controller /// Copyright (c) Opsive. All Rights Reserved. /// https://www.opsive.com /// --------------------------------------------- namespace Opsive.UltimateCharacterController.Traits { using Opsive.Shared.Game; using UnityEngine; /// /// Places the object back in the ObjectPool after the specified number of seconds. /// public class Remover : MonoBehaviour { [Tooltip("The number of seconds until the object should be placed back in the pool.")] [SerializeField] protected float m_Lifetime = 5; private GameObject m_GameObject; private ScheduledEventBase m_RemoveEvent; /// /// Initialize the default values. /// private void Awake() { m_GameObject = gameObject; } /// /// Schedule the object for removal. /// private void OnEnable() { m_RemoveEvent = Scheduler.Schedule(m_Lifetime, Remove); } /// /// Cancels the remove event. /// public void CancelRemoveEvent() { if (m_RemoveEvent != null) { Scheduler.Cancel(m_RemoveEvent); m_RemoveEvent = null; } } /// /// The object has been destroyed - no need for removal if it hasn't already been removed. /// private void OnDisable() { CancelRemoveEvent(); } /// /// Remove the object. /// private void Remove() { ObjectPool.Destroy(m_GameObject); m_RemoveEvent = null; } } }