- Home /
number of colliders inside a trigger.
Is there a variable that keeps track of the number of colliders inside a trigger at any given point in time? I've tried using a variable that increments up and down on the OnCollisionEnter() and OnCollisionExit() functions but OnCollisionExit() doesn't seem to tick it down if the collider object is destroyed while its within my trigger leaving the value a unit above what it should be until i restart. I've tried looking into the Collision class a little bit but it only seems to be a method of passing information onto the OnCollisionEnter() and OnCollisionExit(), without any static instances keeping track of contacts at all points in time.
Answer by xxmariofer · Jun 17, 2020 at 01:03 PM
there is no variable keeping track for the number of colliders iside the trigger, but there is another event called OnCollisionStay() that will get fired every physics iteration so you can count them manually if needed
This seems like it might be what i'm looking for, but i'm having trouble thinking of a way that would keep it updated in real time. If i'm understanding it correctly, i would need to keep track of how many onTriggerStay()'s get called within a given frame, and that would essentially be the number i'm looking for.
I've tried putting collisionCount++; in the onTriggerStay and then collisionCount = 0; in my update function (after where its used) but these don't seem to align themselves really well in realtime and results in CollisionCount flickering between 0 and 1 when it should just be staying at 1.
Im going to try to make a c# list and use ontriggerstay to keep it updated with the gameObject ID's of everything that's in it, that way the size of the list would be exactly the number i'm looking for, but i'm not that familiar with the subsurface code behind c# lists so i don't expect this to go very well.
Would there be a simpler way to achieve the same effect?
Forgot to update: I figured out i can just set a float as a buffer variable and set it to a specific number for every onTriggerStay() and subtract Time.fixedDeltaTime from it every fixed update, so that i have a variable that tells me how much time it was been since the last object left the trigger, this isn't exactly what i was looking for but it works for what i need, ty.
Answer by FrameXdropProvrex · Jun 17, 2020 at 05:39 PM
For me it works perfectly to increment and decrement a value in triggerenter and triggerexit, even when i'm destroying the gameobjects while they're in the trigger (i use this for an AddToInventory script). this is what works for me:
public virtual void OnTriggerEnter(Collider other)
{
StorableItem item = other.GetComponent<StorableItem>();
if (item)
{
collisionCount++;
currentItem = item;
}
}
public virtual void OnTriggerStay(Collider other)
{
if (currentItem != null)
{
if (Grab.stateUp || Pinch.stateUp)
{
InventoryAddBehavior(currentItem, currentItem.gameObject);
}
}
}
public virtual void OnTriggerExit(Collider other)
{
StorableItem item = other.GetComponent<StorableItem>();
if (item)
{
collisionCount--;
}
}
So right now i just have OnTriggerEnter(collision){collisionCount++;} and OnTriggerExit(collision){collisionCount--;} and this mostly works fine, but even without the gameObjects that destroy themselves it still sometimes gets an extra 1 added to it. I'm not sure exactly what causes this because its a much rarer glitch than the one in the post, so i haven't been able to replicate it on command, but i need a solution that will solve both, and the only fix i can think of that will also solve the rarer version of this glitch would be to check it (groundedCount) every frame.
The guy above brought up using onTriggerStay, which sounds like it might be what i need but i'm having trouble thinking of a way to count the objects in my trigger with this function, have any ideas by chance?
Either way thanks for your time.