- Home /
The question is answered, right answer was accepted
How to disable/enable Halo component via script
Is it possible to control the Halo component similar to the other components? Something like:
renderer.enabled = false;
collider.enabled = false;
GetComponent("Halo").enabled = false;
did not work for me aswell ( 'enabled' is not a member of 'UnityEngine.Component'. )
If you're using C#, you need to do "GetComponent().enabled = false;" ins$$anonymous$$d. The one Save referenced you to is a JavaScript-only implementation and the syntax won't be correct for C#.
You need to access the component, then a variable within the component. For example, say you have a script attached to your camera, and you want to disable that camera's "Camera" component. You would by just accessing Component("Camera"), calling the variable, then using enabled = true / false like so (if you're using a string):
GetComponent("Camera").camera.enabled = false;
But notice there is a string in GetComponent? If you DON'T use a string, you could do like so:
GetComponent(Camera).enabled = false;
PLEASE LET US $$anonymous$$NOW IF ONE OF THE ANSWERS HERE PROVIDES A PROPER SOLUTION.
Answer by save · Oct 01, 2011 at 09:16 PM
The Halo component isn't connected to the renderer class. You'd have to do this:
GetComponent(Halo).enabled = false;
If it doesn't work, where are you calling it from and how? Just tested on the GameObject and it works well.
I am calling it in a script attached to a prefab, which will be instantated during gameplay. Prefab also has a collider used as a trigger and a Halo component. I need to disable some components once something hits the trigger collider:
function OnTriggerExit(other : Collider) { Destroy(other.gameObject);
GetComponent("Halo").enabled=false;
renderer.enabled = false;
collider.enabled = false;
}
But I get "BCE0019: 'enabled' is not a member of UnityEngine.Component'. " on line GetComponent("Halo").enabled=false;
PS Sorry I am using a different acount, but I could not log in anymore...
I find it's better to pass a type argument to GetComponent()
.
GetComponent("Halo")
returns a Component
reference.
GetComponent(Halo)
returns a Halo
reference.
The difference is crucial if you're using static typing.
Answer by eormeno · Jul 26, 2013 at 03:39 PM
Component halo = GetComponent("Halo"); halo.GetType().GetProperty("enabled").SetValue(halo, false, null);
Thanks eormeno. Your solution works for me. I'd vote it up but I don't have 15 points yet.
Any idea how to change the size of the halo in script? I only ask because changing this to "Size" doesn't work:
Component halo = GetComponent("Halo");
halo.GetType().GetProperty("Size").SetValue(halo, 0.5f + $$anonymous$$athf.Sin(Time.time), null);