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