- Home /
Flashlight won't turn off.
Hi, I'm trying to get my flashlight turn off when I press R key, so far it doesnt do anything if I press it, not even flicker on/off. Heres the FlashLight.js if someone could point me to the right place where the problem is and maybe even tell me how to fix it.
public var maxBatteryLife : float = 120;
public var flickerStart : float = 5;
public var flickerSpeed : float = 0.1;
public var curBatteryLife : float;
public var batteryTexture : Texture2D;
public var batteryBarTexture : Texture2D;
function Start()
{
curBatteryLife = maxBatteryLife;
}
function Update()
{
if(Input.GetKeyDown("r"))
{
if (this.light.enabled == true);
this.light.enabled = false;
}
else
{
this.light.enabled = true;
}
if(curBatteryLife > 0)
{
if(curBatteryLife > maxBatteryLife)
{
curBatteryLife = maxBatteryLife;
}
else
{
curBatteryLife -= Time.deltaTime;
if(curBatteryLife <= flickerStart)
{
var RandomNumber = Random.value;
if(RandomNumber <= flickerSpeed)
{
this.light.enabled = true;
}
else
{
this.light.enabled=false;
}
}
else
{
this.light.enabled = true;
}
}
}
else
{
curBatteryLife = 0;
this.light.enabled = false;
}
}
function OnGUI()
{
GUI.DrawTexture(Rect(Screen.width - 60, 10, 50, 50), batteryTexture);
var adjustBatteryBar = curBatteryLife * (36/maxBatteryLife);
if(curBatteryLife <= maxBatteryLife/5)
{
GUI.color = Color.red;
}
else
{
GUI.color = Color.white;
}
GUI.BeginGroup(Rect(Screen.width - 55, 28, adjustBatteryBar, 12.7));
GUI.DrawTexture(Rect(0, 0, 36, 12.7), batteryBarTexture);
GUI.EndGroup();
}
Answer by Bunny83 · Jun 23, 2014 at 03:59 PM
You have several problems in your code. First of all Spaghetti code. It's hard to figure out when happens what, Second you use the light's enabled state to tell if the flashlight is (logically) turned on or not. However you also turn it on and off to generate that flicker effect. So you use the same boolean state for two different things. That won't work.
It's best to have a seperate boolean to indicate if the flashlight is on.
// ...
var isOn = false;
function Update()
{
if(Input.GetKeyDown("r"))
{
isOn = !isOn; // toggle it's state
}
light.enabled = isOn;
if (isOn)
{
curBatteryLife -= Time.deltaTime;
curBatteryLife = Mathf.Clamp(curBatteryLife, 0, maxBatteryLife);
if (curBatteryLife <= 0)
{
isOn = false;
}
else if (curBatteryLife < flickerStart && Random.value > flickerSpeed)
{
light.enabled = false;
}
}
}
Thank you for helping me out. And sorry about spaghetti code, I think I should had been commenting it more.
Answer by Landern · Jun 23, 2014 at 03:11 PM
change this IF statement:
if(curBatteryLife > 0)
to this:
if(curBatteryLife > 0 && this.light.enabled == true)
That works almost how I want it to work, but now the problem is that it does turn off, but battery goes to 0 and you can't turn it back on ofcourse then. It shouldnt be draining battery at once if you turn it off.
Your answer
Follow this Question
Related Questions
How to turn off flashlight once in water? 1 Answer
How to turn on a light by pressing and after few seconds turn it off 1 Answer
Using shuriken to create volumetric lights effect 5 Answers
Nothing should be seen without the flashlight pointing at it. 1 Answer
-light.intensity, light.spotAngle and light.color with variable? 0 Answers