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
3
Question by Taai · Nov 17, 2011 at 06:26 PM · c#componenthalo

Enable/disable Halo component in C#

"Component" class has no "enabled" variable, but somehow, in JavaScript this works:

 gameObject.GetComponent("Halo").enabled = true;

How to enable/disable Halo component in C# ?

Yes, I'm not the first one asking this, but nobody has answered to this question yet... I really would like to start using C#, not just stick with Unity's JavaScript.

The Answer:

As Rod Green explained the structure of class inheritance, for "Halo" component, to enable/disable the "Halo" component, you need to access "Behaviour" class.

 (gameObject.GetComponent("Halo") as Behaviour).enabled = true;
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 Rod-Green · Nov 17, 2011 at 08:47 PM

I'm not really sure what you are asking. However if you have a component (MonoBehaviour) of type "Halo" and wish to enable/disable it then all you need to do is.

 Halo thisMono = GetComponent<Halo>();
 thisMono.enabled = true;

I think however you might be getting confused about some of the class structure for the Components and MonoBehaviours in Unity.

Basically the structure goes like this:

 Object
     ->Component
             ->Behaviour
                    ->MonoBehaviour
                           ->Halo (or any other custom class you've set to inherit from MonoBehaviour)

So Halo IS a MonoBehaviour, Behaviour, Component and Object as it's derived from those types in sequence.

What this all really means is that anything that is inherited from Behaviour has access to the .enable property

http://unity3d.com/support/documentation/ScriptReference/Behaviour.html

Comment
Add comment · Show 5 · 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 Taai · Nov 18, 2011 at 08:01 AM 0
Share

Thank you for explaining this structure! As a result, this works: (gameObject.GetComponent("Halo") as Behaviour).enabled = true;

avatar image Rod-Green · Nov 18, 2011 at 09:01 AM 1
Share

try

 gameObject.GetComponent<Halo>().enabled = true;

the allows you to query the component directly by a generic reference. Basically means no casting (converting between types).

avatar image henrypuspurs · Aug 18, 2013 at 05:21 PM 1
Share

Needed this answer, I tried the last comment but it wouldnt work so tried the one before and it did, in case anyones wondering how it would look here is my use of it. void Update() { if(audio.mute) { (gameObject.GetComponent("Halo") as Behaviour).enabled = false; } else { (gameObject.GetComponent("Halo") as Behaviour).enabled = true; } }

avatar image Valentin_B · Apr 24, 2014 at 07:27 AM 0
Share

dunno about the time this was posted, but components are certainly not $$anonymous$$onoBehaviors.

avatar image Rod-Green · Apr 24, 2014 at 07:37 AM 1
Share

Correct! Component != $$anonymous$$onoBehaviour however $$anonymous$$onoBehaviour are derived from / subclass of Component and so inherit from the Component type..

http://msdn.microsoft.com/en-us/library/ms173149.aspx

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Enabling a component of a different gameobject c# (halo) 0 Answers

Enable Draw Halo in Light Component via Script 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Adjusting size of a halo in C# 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