OnTriggerExit triggers randomly while still in trigger zone
I have 2 game objects a player and an enemy. The player has a rigidbody and an AttackRange named collider set as trigger assigned to it as a child object and it has a script that detects enemies in range then puts them in a List, if you move out of range it removes it. The enemy has a collider as well set as a trigger. Now my problem is that even though the enemy is in the AttackRange it randomly removes it from the lost for no apparent reason, I've already wasted 2 afternoons on this looking for answers to my problem, but to no avail.
Here is the AttackRange script.
using UnityEngine;
using System.Collections.Generic;
public class AttackRange : MonoBehaviour {
public List<GameObject> enemiesInRange = new List<GameObject>();
float timer;
// Update is called once per frame
void Update () {
timer += Time.deltaTime;
}
public void OnTriggerEnter(Collider other){
if (other.gameObject.tag == "Enemy" && other.isTrigger == true) {
if(enemiesInRange.Contains(other.gameObject)){
} else {
enemiesInRange.Add(other.gameObject);
print ("Added: " + other.gameObject + " at " + timer);
}
}
}
public void OnTriggerExit(Collider other){
if (other.gameObject.tag == "Enemy" && other.isTrigger == true) {
if(enemiesInRange.Contains(other.gameObject)){
enemiesInRange.Remove(other.gameObject);
print ("Removed: " + other.gameObject + " at " + timer);
timer = 0.0f;
} else {
}
}
}
public List<GameObject> getEnemiesInRange(){
return enemiesInRange;
}
}
Answer by HappyGecko · May 10, 2016 at 09:06 PM
Found this thread and I believe I have a similar problem. My solution is to change the Rigidbody setting 'Collision detection' from Discrete to Continuous which fixed it.
My problem was that the enemy would set the trigger just the once and then after 5 seconds or so it would start spamming the trigger each frame as it sat within the collider.
I just ran into this issue and this workaround was successful for me, too.
$$anonymous$$y scenario: - Large box collider trigger representing an area an object had to be removed from. - A Rigidbody object with a sphere collider that the player pushes out of the area. - The Rigidbody also had a deactivated child object with a trigger collider. Being deactivated this should theoretically have no effect.
Anyhow, on running the scene OnTriggerExit(...) would immediately get called on scripts attached to the Rigidbody's GameObject, even though it (and its children) were still well within the area collider. I checked and confirmed that the trigger it thought it was exiting was the correct one.
After reading the above I then switched the Rigidbody to use Continuous mode, and the problem stopped. Swapping back and forth between Continuous and Discreet reliably causes the problem to return or stop.
This is in Unity 5.5.0f3.
O$$anonymous$$G! I searched my code for errors for hours because the triggerZone registered a lot of objects entering one frame, only to register them as exited in the next (although they were still there). In the end, this solved my problem. I can't understand how this bug uccours in the first place, though! Anyway, thank you!
EDIT: Appearently it wasn't it. It worked randomly once after I changed, but now the problem is back, even with continous collision detection.
Answer by Prophete · Apr 12, 2020 at 03:45 PM
In lot of case problem with collision come with the type of collision you are using (Static Collider, Kinematic Rigidbody Trigger Collider, etc.) This talk about compatibility with each ones : https://docs.unity3d.com/Manual/CollidersOverview.html
For your specific issue, for example, when using a Kinematic Rigidbody Trigger Collider on one GameObject, the other one could be a Static Collider (the GameObject should not have a Rigidbody) and the problem of random exit when still inside the wanted collider will disapear.
Hope it help some people.
Your answer
![](https://koobas.hobune.stream/wayback/20220612001006im_/https://answers.unity.com/themes/thub/images/avi.jpg)