Search code examples
c#unity-game-enginelight

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!


Solution

  • 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");.