mindOfAi
mindOfAi

Reputation: 4622

How can I change the value of 'Draw Halo' inside Point Light - Unity3D

I can't seem to find a Draw Halo property in the intellisense. Is there a way to set its value programmatically? Thanks!

Upvotes: 2

Views: 2310

Answers (2)

Programmer
Programmer

Reputation: 125275

Update:

public Component AddComponent(string className); 

is now deprecated and removed so it can no longer be used to do this. Take a look at the extension method I made, called AddComponentExt that can be used to do this here.

The answer is to use: gameObject.AddComponentExt("Halo");


OLD ANSWER:

Even though this has been answered, I think that other people that will run into this will this useful.

In addition to rutter's answer,

The Halo class cannot be accessed directly like other components.

There are two overloads for the GetComponent function:

public Component GetComponent(Type type);
public Component GetComponent(string type);

And two overloads for the AddComponent function:

public Component AddComponent(Type componentType);
public Component AddComponent(string className);

You have to use GetComponent and AddComponent with the string parameter not the one with the Type parameter.

GetComponent("Halo"); and AddComponent("Halo"); will compile.

GetComponent<Halo>(); and AddComponent<Halo>(); will NOT compile.

In addition, you need to use reflection to toggle Halo on and off by enabling/disabling it.

Extension method that toggles Halo on/off with reflection:

public static class ExtensionMethod
{
    public static void drawHalo(this Light light, bool value)
    {
        //Get Halo Component
        object halo = light.GetComponent("Halo");
        //Get Enable Halo property
        var haloInfo = halo.GetType().GetProperty("enabled");
        //Enable/Disable Halo
        haloInfo.SetValue(halo, value, null);
    }
}

Usage:

Light light = GameObject.Find("Point light").GetComponent<Light>();
light.drawHalo(true); //Extension function. pass true or false

Note:

Make sure to attach Halo to the Light before using this function. Select your light, then go to Component -> Effects -> Halo. You can also do it from script with yourLight.AddComponent("Halo");.

Upvotes: 3

rutter
rutter

Reputation: 11452

Halo is a separate component.

To add a halo to a light: AddComponent<Halo>()

To access the halo attached to a light: GetComponent<Halo>()

The "Draw Halo" checkbox in the inspector is a bit of a red herring -- it creates a Halo component which is then hidden from the hierarchy view, which is boneheaded but it's preserved from older versions of Unity.

Upvotes: 1

Related Questions