OnTriggerEnter() and OnTriggerExit() called multiple times despite checks.

Yes there are many questions and answers concerning similar topics, but funny enough I have not gotten a satisfying answer out of any of those posts.

The title describes the entire problem I am experiencing, so here is the code. By the life of me I do not understand why this is called multiple times when the collider (isTrigger=true) passes through my CharacterController (maybe the problem lies in this?).

Edit: It has come up several times, so to clarify:

  • The EnemyAgent Script does not use
    the damageDealt boolean - it is only
    used to store it as a public variable.

  • There is no other collider apart
    from the CharacterController
    attached
    to the Gameobject in
    question.

  • Also of note might be that the
    triggerenter/triggerexit functions
    are called many, many times and not
    just twice
    .

  • Since there are checks in place the
    single most important question is:
    Why is the OnTriggerExit() method called prematurely - before leaving
    the actual collider?

     using UnityEngine;
     using System.Collections;
     using System.Collections.Generic;
     
     public class HitBox : MonoBehaviour
     {
     	PlayerHealth playerHealth;
     	
     	void Awake ()
     	{
     		playerHealth = this.gameObject.GetComponent <PlayerHealth> ();
     	}
     
     	void OnTriggerEnter (Collider other)
     	{
     		if(other.gameObject.tag == "EnemyWeapon"){
     			EnemyAgent thisAgent = other.gameObject.GetComponentInParent<EnemyAgent>();
     			if (thisAgent.damageDealt)
     				return;
     			if (thisAgent.attacking) {
     				thisAgent.damageDealt = true;
     				playerHealth.TakeDamage (thisAgent.attackDamage);
     				Debug.Log (thisAgent.ID + " in");
     			}
     		}
     	}
     
     	void OnTriggerExit(Collider other){
     		if (other.gameObject.tag == "EnemyWeapon") {
     			EnemyAgent thisAgent = other.gameObject.GetComponentInParent<EnemyAgent>();
     			if (thisAgent.damageDealt) {
     				thisAgent.damageDealt = false;
     				Debug.Log (thisAgent.ID + " out");
     			}
     		}
     	}
     }
    

If you are wondering why the specific enemy agent is only sought after the collider is determined - it’s done this way in order to have several enemies attack the player at the same time.

Please help…

I got this error sometimes and solved it by using a bool to control that it is entering the trigger only one time per collision

I also had multiple calls of OnTriggerEnter and OnTriggerExit
Particulary annoyiing bug.
But I’ve noticed that it was somehow related to dynamic light I had in the scene.
By dynamic light I mean a light source which was moving and changing intensity. It was light from camp fire object.
Enabling this light was causing OnTriggerEnter and Exit to flicker like crazy.
Anyone else had this??

Maybe you can do in just one of OnTriggerEnter() / OnTriggerExit(), not both. And do like this :


class EnemyAgent 
{
     public float GotAttackedDelay = 0.5f;//seconds
     [HideInInspector] public float GotAttacked = 0f;
     ........
     void OnUpdate()
     {
         float timelapse = Time.deltaTime;
         if(GotAttacked > 0)
             GotAttacked -= timelapse;
     }
}

public class HitBox : MonoBehaviour
{
....
     void OnTriggerEnter(Collider other){
         if (other.gameObject.tag == "EnemyWeapon"  ) {
             EnemyAgent thisAgent = other.gameObject.GetComponentInParent<EnemyAgent>();
             if (thisAgent.GotAttacked <= 0) {
                thisAgent.GotAttacked = GotAttackedDelay;
                 Debug.Log (thisAgent.ID + " out");
             }
         }
     }
....

Hope it helps.