UnityScripterAI
Discord ↗
Link Copied to Clipboard
×
"When all objects are destroyed, activate an object"
using UnityEngine; public class ActivateObjectOnAllDestroyed : MonoBehaviour { public GameObject[] objectsToDestroy; public GameObject objectToActivate; private int objectsRemaining; // Start is called before the first frame update void Start() { objectsRemaining = objectsToDestroy.Length; foreach (GameObject obj in objectsToDestroy) { obj.AddComponent<ObjectDestroyedListener>().OnObjectDestroyed += HandleObjectDestroyed; } } private void HandleObjectDestroyed() { objectsRemaining--; if (objectsRemaining <= 0) { objectToActivate.SetActive(true); } } } public class ObjectDestroyedListener : MonoBehaviour { public delegate void ObjectDestroyedAction(); public event ObjectDestroyedAction OnObjectDestroyed; private void OnDestroy() { OnObjectDestroyed?.Invoke(); } }