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.
6
votes
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.
GetComponents
(orGetComponentsInChildren
), 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