6
votes

I am using several box colliders on the same GameObject, the question is, is it possible to refer to each of those colliders separately using
gameObject.getComponent().enabled = false;
Keep in mind that I have few colliders and I want some of them to stay enabled while the others will be disabled.

1
You could find them all with GetComponents (or GetComponentsInChildren), but how would you tell them apart? In circumstances like this, I frequently end up attaching an extra script that keeps track of the other components via lists/arrays that I set up in the inspector.rutter
Yea that is exactly the issue I was hoping that in unity 5 they would already have numbering or naming to colliders ... really frustrating :/Metaldream
you can just save each collider in an array and can then access them one by one. Do you have to know which one is which or do you just one them in arrays so that you can access them using indexes?Programmer
Specifically the problem was solved because I needed only 3 colliders which made it possible to have a poly + box + circle and then it was easy to refer to each one by itself but I'm assuming that for more than 3 colliders a collider array is a mustMetaldream

1 Answers

9
votes

What I do is create empty child GameObjects [One GameObject for every collider] and assign colliders to them, and then I would assign tags to those children [in your case they can be 'AlwaysActive' and 'SwitchingActive' or whatever better name you can come up with].

Then in the parent I will use GetComponentsinChildren<Collider> to find all the colliders, and check if the tag of the GameObject (of a respective collider) matches my requirement or not. If it does I will do my required task otherwise skip it.

NOTE: Colliders on child GameObjects do not call OnTriggerEnter or OnCollisionEnter in parent script. I use delegate strategy to get collisions from child game objects into my parent game object.