// code that is executed when something collides with lock void OnTriggerEnter2D(Collider2D collider) { // ignore any collision with non-player game objects if (collider.CompareTag("Player") == false) { return; } // get the player's script who entered the square PlayerController p = collider.gameObject.GetComponent <PlayerController>(); DoorController d = (DoorController)door.GetComponent(typeof(DoorController)); // set unlock to true if player has keys and matches color if (p.checkKey() && p.colorPower.Contains(color)) { // play sound effect once so it doesn't play on exit if (sound) { keyUnlock.Play(); sound = false; } // set variable and check door unlock = true; d.CheckDoor(); } }