- Home /
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;
}
Answer by aldonaletto · Mar 04, 2012 at 02:51 PM
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.
Thank you for help! I added a light to my muzzle flash and it looks realy good
Potato, I would suggest ticking Aldonaletto's answer as correct so your question is marked as answered. ;)
Your answer
Follow this Question
Related Questions
Turn on/off Script 3 Answers
Lights wont turn back on 1 Answer
Turn off multiple lights trigger 1 Answer
Everything in Unity starts and stops, starts and stops, on and on.. 0 Answers
how to turn off unity answer notification or email?? 1 Answer