// 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(); } } } } }
// code that executes when anything has left the switch void OnTriggerExit2D(Collider2D collider) { // deactivate switch on exit activate = false; // if trigger is keyspawn, deactivate all keys if (triggerObject.CompareTag("KeySpawn")) { KeySpawnScript ks = triggerObject.GetComponent <KeySpawnScript>(); ks.DespawnKeys(); } else if (triggerObject.CompareTag("Gate")) { GateScript gs = triggerObject.GetComponent <GateScript>(); gs.GateClose(); } }