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 WILEz1975 · Mar 15, 2014 at 03:06 PM · inspectorcomponentdisabletype

Disable an unknown type of component at Runtime

I would like to disable a specific component. It seems easy but the type of the component is dynamic, that is a public variable that set on the Unity inspector.

My code:

 public class QualityControl : MonoBehaviour {
 
     public int AtMinQuality;
     public Component myComponent;
 
 void Start () {
 
 if(QualitySettings.GetQualityLevel()<=AtMinQuality){
 
     //myComponent.enabled = false;  <--dont work
     //error CS1061: Type `UnityEngine.Component' does not contain a definition for `enabled'...

     //Component.Destroy(Componente);  <--work but i want disable it, not delete!
        
       }        
 
 }

In simple terms, if my quality setting is less a specific number, i want to disable the component set in the inspector... I can? How?

I try with this code to get the type of my component:

 myTipe=myComponent.GetType();
                 
 GetComponent<myTipe>().enabled = false; <--dont work 

return this error: error CS0118: QualityControl.myTipe' is a field' but a `type' was expected

Where myTipe is a System.Type variable.

Any ideas?

(sorry for my bad english)

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

2 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by whydoidoit · Mar 15, 2014 at 03:07 PM

Don't define it as Component - things that can be disabled inherit from Behaviour so declare it as that.

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 WILEz1975 · Mar 16, 2014 at 05:50 PM 0
Share

If possible i would like to avoid changing the declaration of the component...

I try: ((Behaviour)GetComponent()).enabled = false; In case i want disable the Bloom effect. And work... but, if i cant know the tipe of my component?

myType=$$anonymous$$yComponent.GetType(); ((Behaviour)GetComponent()).enabled = false;

Dont work. Return the error CS0118: QualityControl.myTipe' is afield' but a `type' was expected

avatar image whydoidoit · Mar 16, 2014 at 11:25 PM 0
Share

I can't read your code properly, don't use angle brackets unless it is an indented code block - it makes you point hard to follow right now.

avatar image
0

Answer by WILEz75 · Mar 18, 2014 at 07:58 PM

Solved. Thank you! I simply change Component with Behaviour. No lost the connections on the object in the inspector... (If you have time, read your forum of unity serializer... i have a little problem.)

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

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

22 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

Related Questions

Inspector:Reference a component in Assets, not in scene. 0 Answers

Forcing a type conversion from an abstract class C# 3 Answers

Problem disabling a script 1 Answer

How to disable/enable SSAOEffect or other specific Components? 1 Answer

Custom Editors for any Component 0 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