- Home /
OnTriggerEnter and Exit triggering simultaneously
Ahoy, I have used these two frequently and never ha a particular issue. However I recently started a new project and was trying to do something pretty simple (when a character walks into the trigger, target them.
But for whatever reason when I did it, it began rapid fire enter/exiting over and over again.
So I made a super basic script
void OnTriggerExit (Collider col) {
if (col.tag == "Player")
{
print (col.name);
}
}
void OnTriggerEnter (Collider col)
{
if (col.tag == "Player")
{
print (col.name);
}
}
And when I affixed it to an empty game object with a trigger box it worked fine. So I put it on the character it was supposed to be on. Went back to freaking out and doing both endlessly.
So I tried taking things away until eventually I took away the nav mesh agent (since this is to be an NPC) and suddenly it starts behaving again.
Does anyone have any ideas regarding why/how I could fix this? I had, in a previous project, very successfully attached a similar script to a nav mesh agent. Has changes to the Nav Mesh Agent in v5 made this no longer a thing?
Try printing Time.frameCount in both functions to debug if it's actually happening at the same time. $$anonymous$$aybe you will discover that it enter the trigger in one frame and exits at the next.
Answer by MANDAL0R3 · Sep 11, 2015 at 01:02 PM
So i ran into your problem as well with my project and with a little messing around i figured it out sorta. My AI Character has a Rigidbody attached to him so he can activate Triggers and under the Rigidbody's settings i turned "Is Kinematic " on and now both OnTriggerEnter and OnTriggerExit are called as they are expected to. I'm not really sure why this happens but it seems to fix it for me.
This was the strangest fix in the world..but I had the same problem and it also worked for me. Thanks!
Does anyone know why this works?
Greetings! I was getting crazy with the same problem! it actually helped me a lot!
Answer by lionelmarr · May 31, 2020 at 03:19 PM
Beginners check this: I had a similar result for a completely different (and obvious when you think about it) cause. I had the collider box attached to an animated door object. When the door was triggered, it rotated and took the collider box with it. So all of a sudden, the third person controller that had triggered the box was no longer in the box, despite having not moved. Therefore, the trigger on exit was activated and the door shut again. So the collider returned and triggered the on entry function again which opened the door again. Ad infinitum.
Moral: put the collider box on an empty and not on the animated object. But you probably knew that anyway.
Answer by GraviterX · Mar 06, 2015 at 04:40 AM
Function in scripting are called in order of appearance in the script. Try switching places of your two function lines void OnTriggerEnter and void OnTriggerExit. It should look something like this: using UnityEngine; using System.Collections;
public class NavMeshAgent : MonoBehaviour {
void OnTriggerEnter (Collider col)
{
if (col.tag == "Player")
{
print (col.name);
}
}
void OnTriggerExit (Collider col) {
if (col.tag == "Player")
{
print (col.name);
}
}
}
Be sure to make sure the class name and script name are the same if you are going to change the name of the script. Hope this helps :)
As mentioned, I stripped the Nav $$anonymous$$esh Agent off the character and it works. It has nothing to do with the order of them. But I have to have the nav mesh agent to have AI for the character so I need him to have it.
It seems to be rapid fire exiting and entering the trigger, despite not moving...
Function in scripting are called in order of appearance in the script.
Sorry, but I'm pretty sure this is false. I wrote a test that I believe contradicts it. If you'd like more details, I can elaborate.
Yes, that's absolutely wrong. Switching in CODE will change nothing. Unity called all methods in its own order - simple example "Start()" place this method in your last line of code and it will be called like in any other line...
Lol, this isn't python or powershell.Function in scripting are called in order of appearance in the script.
it wouldn't be true for python or powershell eitherLol, this isn't python or powershell.
Answer by yudistira · Dec 07, 2015 at 08:45 AM
hi, try to removing if (col.tag == "Player") on OnTriggerExit function, it work for me
sorry for my bad english, peace :3
Answer by WRS · Aug 19, 2016 at 12:10 AM
I had same problem today. Fixed it by changing Rigidbody collision detection to Continuous.
Collision detection "Continuous" is significantly more CPU expensive. So you can't use it for many things.
Your answer
Follow this Question
Related Questions
Can't click gameobject when over another trigger? 1 Answer
audio trigger keydown 1 Answer
Trigger Respawn 3 Answers
solution with on trigger destroy 0 Answers