- Home /
OnTriggerEnter firing twice
Hey all, pulling my hair out over this one. I have searched and seen this same topic posted many, many times and tried many workarounds/solutions and none have worked. The more frustrating thing is that my scene is relatively simple, so there aren't a whole lot of places where things could go wrong.
I have two gameObjects I'm working with here - the player, who's only collider is a PlayerController, who has one child object that absolutely does not have any colliders on it. (I have checked and checked again, and even deleted the object), and a Door. The door has a single Box Collider on it that is set as a Trigger and no child objects.
Below is the script on the door. As you can see, am I even disabling and then destroying the door's Collider after executing my script, but it STILL fires a second time. The Debug Log always returns the name of the door and the name of the player.
Please help. I'm going insane, lol.
// Move the player through rooms when he goes through doors.
void OnTriggerEnter (Collider other) {
if(other.gameObject == player) {
Debug.Log ("Trigger at "+gameObject.collider.name+"+"+other.name);
switch(doordirection){
case(DoorDirection.North):
roomController.ChangeRooms(roomController.currentPlayerLocX, roomController.currentPlayerLocY+1);
player.transform.position = new Vector3(0, 1.5f, -14f);
break;
case(DoorDirection.South):
roomController.ChangeRooms(roomController.currentPlayerLocX, roomController.currentPlayerLocY-1);
player.transform.position = new Vector3(0, 1.5f, 14f);
break;
case(DoorDirection.East):
roomController.ChangeRooms(roomController.currentPlayerLocX+1, roomController.currentPlayerLocY);
player.transform.position = new Vector3(-16.5f, 1.5f, 0);
break;
case(DoorDirection.West):
roomController.ChangeRooms(roomController.currentPlayerLocX-1, roomController.currentPlayerLocY);
player.transform.position = new Vector3(16.5f, 1.5f, 0);
break;
}
gameObject.collider.enabled = false;
Destroy(gameObject);
}
Hmm...wonder if moving the player might be firing the second one? Try printing more -- time, position of player.
Since that child object is bugging you, change the player into just a ball (that drops into the door.) $$anonymous$$ake the example simpler and simpler until the double-fire goes away. Then add back until it returns.
Just tested removing the movement, no dice. I added a time and position to the debug and the second activation is happening 0.02 seconds after the first and at the same location.
Answer by Owen-Reynolds · Jul 20, 2014 at 08:07 PM
0.02 is one physics step. Destroy is known to take some time to really destroy it, but enabled=false
should take care of that. Could try SetActive. Or add a bool to prevent refiring.
If it was me, I'd suspect an extra something. Would want to put this script on a fresh cube in a fresh scene, just to be sure I was adding an extra door or something.