Light.enabled = true/false has no effect

I tried the two lines:
void Start()
{
//areaLight.GetComponent().enabled = false;
areaLight.enabled = false;
}
But there isn’t any effect, lights are still enabled
I use 2021.3.11, I started from an empty project, lights still have properties from the imported package
I tried to find fomething into the project properties…

1 Like

I dont understand what you wanna do but if you want just close the light from the beginning of the game put this script on your light


{
   
    void Start()
    {
        this.gameObject.SetActive(false);
    }

    
    void Update()
    {
        
    }
}

you dont have to use the get component, the editor allows you to link the area light for the script and then inside the script you just need to enable or disable the light where you want it as the attached component is already the light component of the light game object

>
>     [SerializeField] Light areaLight;
>     [SerializeField] Light mainWorldLight;
>     void Start()
>    {
>         areaLight.enabled = false;
>         mainWorldLight.enabled = false;
>    }
>

However if you did want to stick with your code and correct it you would have to change the type of areaLight and mainWorldLight variables from Light to GameObject and then the correct version of your code would look like this:

    Light lightComponent = areaLight.GetComponent<Light>();
    lightComponent.enabled = false;

Which actually leads to more work/data because this variable is local to the method and in order to use it persistently youd need to add another pair of variables to the script to hold it, (ie)

[SerializeField] Light areaLightComponent;

Which would alter the start code to look like

    areaLightComponent = areaLight.GetComponent<Light>();
    areaLightComponent.enabled = false;

So in the end its much easier to just use the existing code and finish the methods with code closer to my original reply

@herve3d try this answer for the simplest correction Light.enabled = true/false has no effect - #3 by AJ1126

Privacy & Terms