- Home /
Check if trigger is occupied
Instead of detecting WHEN a trigger is entered, I want to check at any given time whether a trigger is empty or it is occupied by a collider.
Is this possible with C#?
When you searched before posting this question, did you come across: http://alastaira.wordpress.com/2013/12/12/keeping-track-of-targets-in-a-trigger/ or: http://technology.blurst.com/unity-physics-trigger-collider-examples/
?
Answer by Jeff-Kesselman · Jun 03, 2014 at 05:14 PM
bool isOccupied;
void OnEnterTrigger(){
isOccupied=true;
}
void OnExitTrigger(){
isOccupied=false;
}
Was that really so hard?
If you have multiple things that could enter, replace the bool with a count. If you want to know what the things are that have entered, replace the bool with a List
I tried this, but some objects that have colliders in my game use Destroy(gameObject)
, so OnTriggerExit is not called and the bool stays true.
Sorry for not knowing this. I was just wondering if there was a built in parameter. I guess I can work around it by moving the GameObject out of the trigger each time before destroying it.