using UnityEngine; using System.Collections; using Lean.Pool; namespace Invector { [vClassHeader("Destroy GameObject", openClose = false)] public class vDestroyGameObject : vMonoBehaviour { [HideInInspector] public GameObject despawnObject; public float delay = 1f; public UnityEngine.Events.UnityEvent onDestroy; private float timeOffset = 1f; IEnumerator DestroyDelay() { yield return new WaitForSeconds(delay + timeOffset); AudioSource audio = GetComponent(); if (audio != null) { yield return new WaitUntil(() => !audio.isPlaying); } onDestroy?.Invoke(); if (despawnObject != null) { LeanPool.Despawn(despawnObject); } else { Destroy(gameObject); } } void OnEnable() { StartCoroutine(DestroyDelay()); } } }