Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
1
Question by Hamesh81 · Aug 23, 2014 at 12:51 PM · arraycomponentdisable

How can I enable/disable an array of components?

I have an array which using the inspector I would like to add components to that are currently assigned to a gameobject.

 public Component[] playerComponentsToDisable;

Once added I will be calling a function using a foreach to loop through the array and disable all of the components/scripts within it. The problem is that I can't seem to be able to assign any components into the array using the inspector. When I try to select a component for any element of the array, I get a select list of gameobjects instead of components. alt text

Using the standard 3rd Person Controller as an example, I would like to add its Character Controller component and Third Person Controller component into the array via the inspector. I am not assigning them via GetComponent because the script with the array is going to be assigned to many different gameobjects and so it would be a lot of extra coding for each, compared to simply selecting the components needing to be disabled via the inspector. Am I missing something, or can this not be done in this way? I haven't tried using a list yet but shouldn't this work for either?

selectcomponent.png (37.3 kB)
Comment
Add comment · Show 1
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 Hamesh81 · Aug 23, 2014 at 02:11 PM 0
Share

Thanks for that explanation; an array of strings will definitely do what I need. If you would like to convert your comment to an answer I can accept it.

2 Replies

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

Answer by _dns_ · Aug 23, 2014 at 01:48 PM

Hi, if I understood well your problem: I think it's caused by the fact that a Component is not made to exist by itself: it has to be contained by a GameObject. Then, when referencing a Component in the Inspector, Unity references the GameObject that contains the Component and not the Component itself (EDIT: this may be over simplified and even false, I guess it also has to do with Types and derivation as it would work with an array of "MyMonobehaviour" instead of an array of Component[] containing any component-derived type)(EDIT2: must be because Unity must know what type is the class it deserializes and then can't serialize an array of some base class containing derived classes... hummm, my brain's Start() wasn't called correctly this morning :-)

If you want to enable or disable Components in some GameObjects in your scene based on a list you can edit in the inspector (that's what I understood from your question): one solution could be to store strings. In the inspector, you could store an array of strings that you would each format like "gameobjectname.componenttype". Then, in the code, use the string[] to call GameObject.Find("") and then GetComponent("") (after splitting each one). It may not be very convenient if you have a lot of components to enable and disable but quite quick to code. You could also "cache" those lists into a Component[] during the Start() so it's faster to enable/disable during the game.

Comment
Add comment · 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
3

Answer by jstn-as · Jan 07, 2019 at 04:58 PM

Hi, sorry for necroposting but this is the link that came up first, but I think I got a more elegant solution to the problem.

 // Create an array of Behaviours, which are Components that can be enabled/disabled.
 public Behaviour[] _componentsToDisable;
 
 // Then you can disable them whenever you want.
 foreach (var component in _componentsToDisable)
 {
     component.enabled = false;
 }

Hopefully this works better!

Comment
Add comment · Show 3 · 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 _dns_ · Jan 07, 2019 at 05:22 PM 0
Share

Your code is correct, and it answers the question "How can I enable/disable an array of components"

Though, the full question describes a situation more complicated that needed a different solution. Your answer might help other people as the question's title vs question's content can be misleading.

avatar image Arano · Aug 28, 2019 at 09:47 AM 0
Share

this does not anwser the question but just changes it to something else... components includes things such as renderers (renderer.enable does exist) but u can not add a renderer to a list of Behaviours, this is why we want components. i just want a complete solution to be able to activate components.

avatar image Bunny83 Arano · Aug 28, 2019 at 10:13 AM 1
Share

"components" can not be enabled since the Component class does not have an enabled property but several subclasses have. If you want a single array of Components there's no way around some type checking and casting. Though there are only a few types you have to check. A more or less complete list can be found in my comment over here. It checks for Behaviour, Renderer and Collider which are the 3 major base classes which covers almost every built-in component which can be enabled. In addition if all 3 fail we use reflection to figure out if the component has an enabled property.

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

26 People are following this question.

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

Related Questions

Unable to enable script 0 Answers

script communicate with each other 0 Answers

Problem disabling a script 1 Answer

problem with FindWithTag 2 Answers

enable/disable specific components 3 Answers


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