Disable array of components
Dear community,
I've been searching several forums for the answer to my question. There have been similar issues, but mine is a bit different, and I can't seem to find a solution or workaround:
I cannot find a way to disable several Collider2D on my game object. When people online ask why enabling/disabling a component doesn't work, the answer is usually: "Use GetComponent(); to assign the component". This is easily achieved for one component:
Collider2D coll = this.GetComponent<Collider2D> ();
coll.enabled = false;
This code works. However, I need it to work for several colliders; let's say, two:
Component[] colls = new Component[2];
colls = this.GetComponents <Collider2D>();
for (int i = 0; i <= 1; i++) {
colls [i].enabled = false;
}
This code produces the following error: error CS1061: Type 'UnityEngine.Component' does not contain a definition for 'enabled' and no extension method 'enabled' of type `UnityEngine.Component' could be found (are you missing a using directive or an assembly reference?)
This is really weird to me because UnityEngine.Component clearly DOES contain a definition for "enabled", since it works with the other syntax. I then decided to try something else and declared:
public Component coll1;
public Component coll2;
I assigned the colliders by dragging and dropping them in the Inspector. However, when calling
coll1.enabled = false;
coll2.enabled = false;
I got the same error as with the array above.
My questions are: 1. Could it be that ".enabled" only works on a Component if it's assigend with the "GetComponent()" command? (And wouldn't that be really weird and annoying?) 2. If so, is there a way for me to assign the two colliders this way and disable them?
Thanks a lot in advance, guys!
Answer by Bunny83 · Oct 26, 2016 at 11:20 PM
No, "Component" does not contain an enabled property. Only certain components have one. Specifically all classes which are derived from Behaviour and some others implement an enabled property. However the Component class itself does not have such a property.
Since you perform an implicit down cast (Collider2D to Component) you can't access the enabled property that is defined inside the Behaviour class from which Collider2D is derived from.
You should declare your array as Collider2D array
Collider2D[] colls = GetComponents <Collider2D>();
for (int i = 0; i < colls.Length; i++) {
colls[i].enabled = false;
}
Or you could simply declare the array like this:
var colls = GetComponents <Collider2D>();
The generic GetComponents will return an array of the type you pass as generic type argument. So GetComponents <Collider2D>()
will return Collider2D[]
ps: You first create an array with two elements and then you reassign the variable to the array returned by GetComponents so the first array is lost. It's not necessary in the first place.
Thank you very much! I'm still not 100% I understand, but working with a Collider2D array and using its "enabled" property does the trick!
Edit: As for the redundant declaration, I changed it to this:
Collider2D[] colls = this.GetComponents <Collider2D>();