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 /
avatar image
3
Question by dusker · Jul 29, 2011 at 04:45 PM · arraycomponentenabled

Disable all components but 1 on a gameobject

 Component[] comps = this.gameObject.GetComponent<Component>();
 for(int i=0;i<comps.length;i++)
 {
   Component c = comps[i];
   Type t = c.GetType();
   if(t.FullName == "TrackRenderer")
   {
     this.gameObject.GetComponent<TrackRenderer>().enabled = true;
   }
   else
   {
         this.gameObject.GetComponent<c>().enabled = false;
   }
 }

The error I get with this is: The type or namespace name 'c' could not be found.

I have a few gameobjects with this script attached to it, each one has a few different scripts attached to it, but they always have a TrackRenderer. The thing I want to do is disable all those scripts and keep the TrackRenderer enabled. Anyone have suggestions?

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
6
Best Answer

Answer by Herman-Tulleken · Jul 29, 2011 at 05:17 PM

The reason you get the error is that c is a variable, and not a type. Here is one way to do it: set all components inactive, and then simply switch on the TrackRenderer.

Note, you don't have to reference this.gameObject, you can use the method inherited from MonoBehaviour straight.

Edit: Incorporated the comments below.

 MonoBehaviour[] comps = GetComponents<MonoBehaviour>();

 foreach(MonoBehaviour c in comps)
 {
   c.enabled = false;
 }

 GetComponent<TrackRenderer>().enabled = true;
Comment
Add comment · Show 10 · 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 dusker · Jul 29, 2011 at 05:23 PM 0
Share

Thanks for the quick reply and tips!

At the c.enabled = false line I get the following error:

Type 'UnityEngine.Component' does not contain a definition for enabled

avatar image SisterKy · Jul 29, 2011 at 05:33 PM 0
Share

ah, yes that's right... enabled is not a variable of 'Component' but a feature of 'Behaviour', 'Collider', 'Particle Emitter' and 'Renderer' only (unless I missed one)... other Components can't be enabled/disabled ...

You could use if (c.enabled != null) {c.enabled = false} , I think?

Greetz, $$anonymous$$y.

avatar image dusker · Jul 29, 2011 at 05:33 PM 0
Share

I also noticed that if I leave the line: Component[] comps = GetComponent();

I get an error: Cannot implicity convert type 'UnityEngine.Component' to 'UnityEngine.Component[]'

but if I switch GetComponent to GetComponents, I get the enabled error

avatar image SisterKy · Jul 29, 2011 at 05:37 PM 0
Share

hmm... looks like a little bug to me. Error-message should be: 'Cannot implicity convert type 'UnityEngine.Component' to 'UnityEngine.Component [ ]', I think... Greetz, $$anonymous$$y.

avatar image SisterKy · Jul 29, 2011 at 05:49 PM 1
Share

edit: nonsense, I made a thinking-error.
Of course the if-statement won't fix the error...
just because the derived classes of your components do have the 'enabled' variable doesn't mean we can access it.
After all, we just collected the non-derived component-class which simply still doesn't have 'enabled'. ^.^'

if all the components are behaviours, then collect the $$anonymous$$onoBehaviours ins$$anonymous$$d of components.


$$anonymous$$onoBehaviour[] behavs = GetComponents< $$anonymous$$onoBehaviour >();
foreach($$anonymous$$onoBehaviour c in behavs)
{
  c.enabled = false;
}

This way, you probably won't need
GetComponent().enabled = true;
because renderer isn't $$anonymous$$onoBehaviour and won't be disabled in the first place.

Show more comments

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

enabled is not a member of UnityEngine.Component 1 Answer

Access Animations array in Animation component 1 Answer

Disabling GameObject doesn't disable components 0 Answers

Search for specific entity in an array? 1 Answer

Disabled script still working C# 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