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; void Start() { objectsRemaining = objectsToDestroy.Length; for (int i = 0; i < objectsToDestroy.Length; i++) { objectsToDestroy[i].GetComponent<ObjectDestroyer>().OnObjectDestroyed += HandleObjectDestroyed; } } private void HandleObjectDestroyed() { objectsRemaining--; if (objectsRemaining <= 0) { objectToActivate.SetActive(true); } } } public class ObjectDestroyer : MonoBehaviour { public delegate void ObjectDestroyed(); public event ObjectDestroyed OnObjectDestroyed; private void OnDestroy() { if (OnObjectDestroyed != null) { OnObjectDestroyed(); } } }