public void ToggleRandomViableInteractable() { // Gather a list of all interactables that could be turned on or off List <Interactable> viableInteractables = new List <Interactable>(); for (int i = 0; i < interactables.Count; i++) { Interactable ia = interactables[i].GetComponent <Interactable>(); // Interactable script is present, it's gameobject is in the scene and the bad power state is not already active if (ia != null && ia.isActiveAndEnabled && ia.gameObject.activeInHierarchy && ia.isPowered != ia.badPowerState) { viableInteractables.Add(ia); } } // Pick random viable interactable and turn it into it's bad power state if (viableInteractables.Count > 0) { Interactable selectedInteractable = viableInteractables[Random.Range(0, viableInteractables.Count)]; selectedInteractable.ActivateBadPowerState(true); } else { Debug.LogWarning(gameObject.name + ": All viable interactables are already in their bad power state!"); } }