- Home /
How do I prevent multiple triggers using OnTriggerEnter?
Object A has a trigger collider on it.
Object B has a trigger collider and a normal collider (not a trigger).
When Object A collides with Object B using OnTriggerEnter, the function produces 2 triggers. How do I detect only Object B's normal collider?
Answer by rhodes23 · Jan 30, 2017 at 11:04 AM
OnTriggerEnter(Collider col) allows you to store a reference to the colliding gameObject/collider. Inside you can simply check if the collider is the one you want to detect and run your code based on that result. For example:
void OnTriggerEnter(Collider col){
if(!col.isTrigger){
DoStuff();
}
}
When doing it like this, DoStuff() is only executed when the colliding game object's collider is not set to "isTrigger". This is just an example. You can, of course, check for other properties to identify the object, like name or position.
Hope this helps.
Thanks for your response. I completely forgot about the .isTrigger check.