Figuring out exactly how many objects are touching a trigger (or collider)

I need to know how many objects with a certain tag are touching a particular object at any given time. Right now I’m using “OnTriggerEnter” and “OnTriggerExit” to achieve this - but its not working.

OnTriggerExit seems to be the culprit. Ive noticed through testing that OnTriggerExit is only called at the moment that NO objects are touching the trigger. This means that if one object moves out of the collision bounds of the trigger while another is still touching it, the game won’t realize that the trigger is touching one less object. This is leading to all kinds of chaos and improper functioning of my game. Is there an alternative to OnTriggerExit() that will serve my purposes?

Instead of using OntriggerExit try to use OnTriggerStay() that might help you.

It depends on if your collider is in fact a trigger or not. If it is a trigger:

var touching : int = 0;

function OnTriggerEnter( who : Collider )
{
  touching++;
}

function OnTriggerExit( who : Collider )
{
  touching--;
}

…That’s the correct code if you’ve got just a trigger or just a collider on an object. If you have multiple colliders…things get hairy (like is happening in Zombies vs. Knights, with my AI system. It led to me cutting Aura buffs out of that game.)