// code that executes when anything has touched the switch void OnTriggerEnter2D(Collider2D collider) { // only deal with objects that are players or boxes if (/*collider.CompareTag("Player") ||*/ collider.CompareTag("Box")) { // if the correct player or box is on switch, activate and check switch controller if (collider.name.Contains(color)) { activate = true; if (triggerObjects[0].CompareTag("KeySpawn")) { foreach (GameObject triggerObject in triggerObjects) { KeySpawnScript ks = triggerObject.GetComponent <KeySpawnScript>(); ks.CheckAllSwitches(); } } else if (triggerObjects[0].CompareTag("Gate")) { foreach (GameObject triggerObject in triggerObjects) { GateScript gs = triggerObject.GetComponent <GateScript>(); gs.GateOpen(); } } } } }
// external function to check all switches public void CheckAllSwitches() { // for each switch, get their script and check their status foreach (GameObject obj in mySwitches) { SwitchScript ss = obj.GetComponent <SwitchScript>(); // if false, can return immediately since all switches need to be true if (ss.CheckSwitch() == false) { return; } } // if all switches are pressed, open gate GateScript gs = gate.GetComponent <GateScript>(); gs.GateOpen(); }