- Home /
FlashLight Turning off, but not back on again? (Simple Script Fix?)
Hello me again. all i want to do is turn the "f" button on my keyboard into a flashlight button, so the lights on my Gun turn on and off. but the problem I'm getting, is that they turn off, just not back on again? here's my script:
var hazerdLights1 : Light; var hazerdLights2 : Light; var flashlight : Light;
private var defaultRange1 : float; private var defaultRange2 : float; private var defaultRange3 : float; private var flashlightOff = false;
function Start () { defaultHazerdRange1 = hazerdLights1.range; defaultHazerdRange2 = hazerdLights2.range; defaultFlashlightRange = flashlight.range; }
function Update () { if(Input.GetButtonDown("Torch") && !flashlightOff) { hazerdLights1.range = 0; hazerdLights2.range = 0; flashlight.range = 1; flashlightoff = true; } else if(Input.GetButtonDown("Torch") && flashlightOff) { hazerdLights1.range = defaultRange1; hazerdLights2.range = defaultRange2; flashlight.range = defaultRange3; flashlightoff = false; }
}
I'm still a very big noob with this coding business, but i thought this seems ok. i get no error from the console, yet all i can do, is turn the light's off, not back on again?
any help, like always, is extremely helpful.
Answer by duck · Nov 17, 2010 at 12:49 PM
Typically you'd switch a light on and off with a script placed directly on the light, negating the need for variable references, like this:
function Update() {
if (Input.GetButtonDown("Torch")) {
light.enabled != light.enabled;
}
}
However, if you want to control two lights, from a script placed on a third object, you need variable references to the light objects (as you had set up), and just control them in a similar way, like this:
var hazardLight1 : Light; var hazardLight2 : Light;
function Update() { if (Input.GetButtonDown("Torch")) { hazardLight1.enabled = !hazardLight1.enabled; hazardLight2.enabled = !hazardLight2.enabled; } }
I'm not entirely clear what you were trying to do with the other light, but using code similar to this you should be able to do whatever you want with it.
The above script toggles both lights, so it kind of assumes you have them starting either both off, or both on. If you have them starting with one off and one on, hitting the Torch button will switch one off and the other on!
Nope...still don't work. comes up with errors "Expressions in statement must be executed for the side effects" or something along the lines.
Answer by oliver-jones · Nov 17, 2010 at 11:29 AM
Humm,
Just try replacing the second (Input.GetKeyDown("f)) with a different Key ... say 'd' and see if that will work (for debugging)
So: F = On D = Off
Also, make sure in your Input (Edit > PrjectSettings > Input) has a define for the key input ... but I don't think that will make a difference to be honest.
Answer by oliver-jones · Nov 17, 2010 at 12:06 PM
If you get it to work, then let me know please
$$anonymous$$y apologies, still kind of new around here
Answer by oliver-jones · Nov 17, 2010 at 10:45 AM
I have done something like this. I have a flash light on my assault rifle which can be turned on or off by pressing the F key. I have done it slightly cheaper to you. But mine works :)
This is what I do: (I do not have my Unity in front of me, so I can't test it 100%)
var FlashLight : Light; //drag your light object onto here var FlashLightOn = false; var LightToggle = false;
function Start() { FlashLight.enabled = false; }
function Update() { if(Input.GetKeyDown("f") && LightToggle == false) { FlashLight.enabled = true; LightToggle = true; }
//You might want to put an else if -- check tho if(Input.GetKeyDown("f") && LightToggle == true) { FlashLight.enabled = false; LightToggle = false; } }
(again, not tested)
Thanks. i still don't know all the API's of unity yet. Just disabling the light seems a better idea :D
Your answer
Follow this Question
Related Questions
toggle projector as light (C#) 0 Answers
Flash light toggle problem 1 Answer
help with Flashlight Pickup 1 Answer
How to make a flashlight toggleable in C#? 6 Answers
How to make a Flashlight 5 Answers