- Home /
How to enable/disable two box colliders in one object ?
hi all
How to enable/disable two box colliders in one object ?
i have 2 box colliders :
1- box collider has isTrigger
and other one
2- box collider
i have code but only enable/disable "boxcollider isTrigger" and no affect on secend boxcollider !
my code is :
this.collider.enabled = true/false;
how can i enable/disable Both box colliders ?
I have just tested that in empty test scene and it does work for me. But also to get your code to work, I have to put
gameObject.GetComponents<BoxCollider>().enabled
= false;
ins$$anonymous$$d of your this.collider.enabled = false;
not much different in JS
var myColliders: BoxCollider[] = gameObject.GetComponents.<BoxCollider>();
for (var bc : BoxCollider in myColliders) bc.enabled=false;
Answer by tomekkie2 · Dec 08, 2013 at 08:06 AM
Looks like this.collider references the first of your colliders. You should use:
BoxCollider[] myColliders = gameObject.GetComponents<BoxCollider>();
to get an array of colliders attached and loop through the array.
foreach(BoxCollider bc in myColliders) bc.enabled = false;
Is it possible to make an array of different Collider types and turn each one off in an array as well?
Answer by jchart7 · Aug 24, 2014 at 06:12 PM
if you need to turn off / on Sprite collider inside a function use
gameObject.collider2D.enabled = false;
Your answer