- Home /
on trigger empty
Is there any way to make event like If(trigger is empty)...?
In my situation, in trigger could be more that 20 objects, that entering & leaving my trigger randomly. So when trigger is absolutely not triggering any objects - is empty, it's turning in to a collider.
Maybe also checking that every 2 seconds.
Answer by Tim-Michels · Jun 27, 2012 at 11:42 AM
The function OnTriggerStay detects colliders inside the triggers.
In the OnTriggerStay function, you could do a ++counter. Then, you have a seperate coroutine, which checks the counter every x seconds, whereafter placing it back to 0. (this coroutine should also keep looping)
That way you can count the objects that are in your trigger.
Let me know if it works ;)
it works, but it eating FPS, in my scene fixed update time is very small... I did it with OnTrigger enter ++, and OnTriggerexit --, when counter is 0, it tells that inside is empty, but is there any way to do it with OnTriggerStay function with increasing fixed update time for it? - Like to check it eery 1 second, not every millisecond...
Answer by AlexFuture · Feb 01, 2013 at 01:16 PM
I have been using an OverlapSphere in my game warscape alpha for error checking in these cases and it works well (network players leaving server in middle of automatic doors leaves the doors broken and the trigger count useless). Just check the returned collider arrays length for zero (use a LayerMask too). you could invoke this function every few seconds.
Your answer
![](https://koobas.hobune.stream/wayback/20220613073349im_/https://answers.unity.com/themes/thub/images/avi.jpg)