unity-game-engineunityscriptunity3d-2dtools

Unity multiple collider on same object


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.


Solution

  • 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.