Battery life after the game ends PROBLEM

Hi!
I have a problem with my battery power, for the flashlight, after the game ends. At first when you start the game it’s fine, but when you finished the game and comes to the main menu and then restart it, the battery power is the same as it was when the game ended. It only works if you reload the game but that’s not good.
Please help!

11354-battery.jpg

Flashlight script:

var BatteryPower : Texture;
var lightInner : Light; //Connect the light source in the Inspector
var lightOuter : Light; //Connect the light source in the Inspector
var FlashLightSound : AudioClip;
static var energy : float = 100; //The energy amount of the flashlight
static var turnedOn : boolean = false; //Boolean to check whether it's turned on or off
private var drainSpeed : float = 0.3; //The speed that the energy is drained

function Update () {
    if (Input.GetKeyDown(KeyCode.F)) ToggleFlashlight();
    
    
   
    if (Input.GetKeyDown(KeyCode.F)) {

	audio.PlayOneShot(FlashLightSound);
	
	}
    
}



//When the player press F we toggle the flashlight on and off
function ToggleFlashlight () {
    turnedOn=!turnedOn;
    if (turnedOn && energy>0) {
       TurnOnAndDrainEnergy();
    } else {
       lightInner.enabled = false;
       lightOuter.enabled = false;
    }
    
}


function OnGUI () {
    GUI.BeginGroup( Rect(40, 10, 218 * (Flashlight.energy/100.0) +35 , 90) );
    GUI.Label( Rect(0, 0, 256, 128), BatteryPower );
    GUI.EndGroup();
}

//When the flashlight is turned on we enter a while loop which drains the energy
function TurnOnAndDrainEnergy () {
    lightInner.enabled = true;
    lightOuter.enabled = true;
    
    while (turnedOn && energy>0) {
       energy -= drainSpeed*Time.deltaTime;
       yield;
    }
    lightInner.enabled = false;
    lightOuter.enabled = false;
}

//This is called from outside the script to alter the amount of energy
static function AlterEnergy (amount : int) {
    energy = Mathf.Clamp(energy+amount, 0, 100);
}

Sounds like you’re staying in the same level in your build, possibly? If you’re not wanting to load a new level, try resetting any variables that need to be reset at the “level end” function, whatever that may be. Otherwise, if you load a new level, or reload the current level, your scripts will be reset. Check out Application.LoadLevel

EDIT:

Don’t use static variables. If you want something to keep its data, use DontDestroyOnLoad instead.

To access something, just have an empty game object in your scene with the scripts attached that you want to access. EXAMPLE:

Say you have a gameobject named (scripts), and a script attached named (testscript). In that script, you have a variable (var testVar : int = 0;). You want to change that.

var scriptObject : GameObject; (drag your scripts object into the inspector)

scriptObject.GetComponent(testscript).testVar = 100;