Unity – Enabling and Disabling Components

Attach this code to a light:

#pragma strict

private var myLight : Light;


function Start ()
{
    myLight = GetComponent(Light);
}


function Update ()
{
    if(Input.GetKeyUp(KeyCode.Space)) // click space bar to toggle light on/off
    {
        myLight.enabled = !myLight.enabled; // invert the status using not keyword
    }
}

Statemets:

myLight.enabled = myLight.enabled; -> light is enabled

myLight.enabled = !myLight.enabled; -> light is disabled, you can use not keyword ‘!’