turning on spotlight

Hi, I am new to Unity, and am trying to turn on a spotlight when I hold down the left mouse button and then when I release the button, the light should turn off again.

code I have right now:

var SpLight : Light;
    
    function Start(){
    SpLight.active = false;
    }
    
    function Update (){
    
        if(Input.GetMouseButtonDown(0)){
        SpLight.active = true;
        }
        else
        {
        SpLight.active = false;
        }
    }

Also the spotlight is a child of the main camera of the first person controller.

When I playtest, spotlight is off but when holding down the mouse button nothing happens.

GetMouseButtonDown only returns true for one frame, when you first press it. Use GetMouseButton to continuously return true.

var SpLight: Light;

function Update() { 

 if(Input.GetButtonDown("Fire1"))
Down();
 if(Input.GetButtonUp("Fire1"))
Up();
} 

function Up(){
print("Up");
 SpLight.enabled =false; 
}

function Down(){
print("Down");
 SpLight.enabled = true; 
}

Thats one version of what you want. I find it a bit easier to understand if I farm out functions from the update zone. If I do too much inside the Update function,(Ie all happening in one frame) things get confusing. I dont know if my method is an ideal way, but hey it works…

HTH
AC

Thanks Targos for the help. Is there a difference between ‘enabled’ and ‘active’?

If you look in the inspector window, active=false; is like turning of the top checkbox, and everything is off, so scripts on this object wont work and scripts referncing this object will throw null reference errors.

enabled = false is in this case turning off the light component only- We specified light in the script we wrote- (it has is own checkbox in the inspector), so scripts will still exectute and the other components are still functional…

Follow?
Cheers
AC