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.

- 125
- 1
- 1
- 9
-
2You 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 May 08 '15 at 21:13
-
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 May 08 '15 at 21:31
-
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 May 09 '15 at 00:01
-
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 must – Metaldream May 13 '15 at 09:18
3 Answers
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.

- 1,036
- 8
- 23
-
"NOTE: Colliders on child GameObjects do not call OnTriggerEnter or OnCollisionEnter in parent script." --- This was the information I was looking for, thanks! – Markus Weninger Sep 06 '20 at 11:35
You can generate all colliders by script
var collider1 = gameObject.AddComponent<BoxCollider>();
var collider2 = ...
...
then you can set all properties for each collider
https://docs.unity3d.com/ScriptReference/Collider.html
So you can use every collider by it variable as you want

- 11
- 3
Old but maybe it helps: you can get multiple Components in a gameObject:
Collider[] colls = GetComponents<Collider>();
foreach(var col in colls){
col.isTrigger = false;
}