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 ?

down't work

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; instead of your this.collider.enabled = false;

can put javascript code ?

not much different in JS var myColliders: BoxCollider[] = gameObject.GetComponents.<BoxCollider>(); for (var bc : BoxCollider in myColliders) bc.enabled=false;

tanQ very much this is work !!!!!!!!! tanx man

2 Answers

2

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?

if you need to turn off / on Sprite collider inside a function use

gameObject.collider2D.enabled = false;