i have a flashlight script that has a bug

i have this script that i have been working on, and improving. it runs good as far as turning on and off and picking up battery power where where it left off. when the light runs to a certain point, it starts to fade the light.range, and that is all good, except… if i shut off the light during the time the light.range is fading and turn it back on, it starts all over again from the maximum range. does any one have a suggestion. and if your in the mood, I tried to add a flickered light function, but it just flickers the whole time during the light.range fade.

#pragma strict
var batteries: float = 2.0f;
var batteryLife: float = 420.0f;
var batteryReductionSpeed: float = 1.0f;
//var bat: int = 0;
var lightRange: float = 100.0f;

//var flashLightOn: boolean = false;

function Awake () 
{
	GameObject.FindWithTag("HeadLamp").light.enabled = false;
	batteries--;

}

function Update () 
{	//bat = batteries + batteryLife;
	if(GameObject.FindWithTag("HeadLamp").light.enabled == true)
	{
		  //batteryLife = batteryLife - batteryReductionSpeed;
		batteryLife = batteryLife -(batteryReductionSpeed * Time.deltaTime);
		//GameObject.Find("FlashLightLight").light.range = lightRange;
	}
		if(Input.GetKeyDown("o") && !GameObject.FindWithTag("HeadLamp").light.enabled == true && batteries >= 0) 
		{
			if(batteryLife <= 0 && batteries > 0)
			{
				batteries--;
				batteryLife = 420;
			}
			GameObject.FindWithTag("HeadLamp").light.enabled = true;
			GameObject.Find("FlashLightLight").light.range = lightRange;
		}
		//bat = batteries + batteryLife;
		
		else if(Input.GetKeyDown("o") && GameObject.FindWithTag("HeadLamp").light.enabled == true || batteryLife == 0)
	{
		
		GameObject.FindWithTag("HeadLamp").light.enabled = false;
	}
	if(batteryLife <= 0)
		{
			batteryLife = 0;
			GameObject.FindWithTag("HeadLamp").light.enabled = false;
		}
		if(batteryLife <= 100)
		{
			//LightFlicker();
			//GameObject.Find("FlashLightLight").range = batteryLife = batteryLife - (batteryReductionSpeed * Time.deltaTime);
			  GameObject.FindWithTag("HeadLamp").light.range = GameObject.Find("FlashLightLight").light.range -(batteryReductionSpeed * Time.deltaTime);//batteryLife = batteryLife - (batteryReductionSpeed * Time.deltaTime);
			  //GameObject.Find("FlashLightLight").light.range = lightRange;
		}
	
	}

Your problem is on line 33 in the listing above. When you turn your flashlight back on, you set its light range to ‘lightRange’. That is, back to the full range of the flashlight. When you turn the flashlight off (line 40), you should add a line that saves the current range of the flashlight and restore that range on 33 when it is turned back on.

I’m trying to figure out how to do that… i created a variable curLightRange, and thought by adding after line 40, and assigning lightRange to curLightRange, but that did not work. and then i thought …
instead of…GameObject.Find("FlashLightLight").light.range = lightRange;
to…GameObject.Find("FlashLightLight").light.range = curlightRange; but that does’t work either… i might be confusing myself…thanks for any help

your a genius. that flashLightLight was the light itself. I had a feeling that was wrong, but it is all a learning process. i should have trusted my gut about that line. thanks, i bow to your programming genius