Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
2
Question by Mjeno · Oct 26, 2016 at 10:30 PM · c#bugcollider2dcomponentenable

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
3
Best Answer

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[]

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Oct 26, 2016 at 11:22 PM 0
Share

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.

avatar image Mjeno · Oct 26, 2016 at 11:37 PM 0
Share

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>();

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Physics.Linecast does not return collider between start and end. 1 Answer

NullReferenceException: Object reference not set to an instance of an object 1 Answer

How to write type-safe loosely coupled components 1 Answer

Script is losing component on runtime 1 Answer

Cant Disable Script on Another Gameobject 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges