How to turn on a light by pressing and after few seconds turn it off

Hello, im making a muzzle flash and I want to add a light to it. So what I want to do is that the light would turn on when I press the button and after some time the light would be turned off. Im not sure what is wrong with the script… Thank you for answering.

var LightLength = 0.1;

function Update() {

if(Input.GetButtonDown("Jump")) {

  LightLengthOn();

}

}

function LightLengthOn()

{

LightLength.active = true;

yield WaitForSeconds(LightLength);

LightLength.active = false;

}

Dear God! Are you trying to turn the float variable LightLength active and inactive? You should use light.enabled instead (script attached to the light game object):

var LightLength = 0.1;

function Update() {
  if(Input.GetButtonDown("Jump")) {
    LightOn();
  }
}

function LightOn() {
  light.enabled = true;
  yield WaitForSeconds(LightLength);
  light.enabled = false;
}

But be aware that this will only illuminate the nearby objects. To show the muzzle flash you will need a muzzle object, and turn it on and off like the muzzle light. You can download the FPS Tutorial and “steal” the muzzle flash object from the machine gun model, then child it to your own weapon and use renderer.enabled to turn it on and off.