- Home /
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;
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
Thank you for explaining this structure! As a result, this works: (gameObject.GetComponent("Halo") as Behaviour).enabled = true;
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).
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; } }
dunno about the time this was posted, but components are certainly not $$anonymous$$onoBehaviors.
Correct! Component != $$anonymous$$onoBehaviour however $$anonymous$$onoBehaviour are derived from / subclass of Component and so inherit from the Component type..
Your answer
Follow this Question
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