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;
}
}