- Home /
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!
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);
}
I would say you just have to reset all the related variables when you start a new game.
Btw. Is this the rights script because it says something about safe and nothing about a flashlight or batteries.
So i think it's what i though it will be. You have to call a function when you start a game which resets the energy to 100. $$anonymous$$aybe you have to reset some other variables too.
Ok, do you have any idea how I can do that. I'm not that great with coding :)
Well there is this function which gets called when the level is loaded: http://docs.unity3d.com/Documentation/ScriptReference/$$anonymous$$onoBehaviour.OnLevelWasLoaded.html
Although normally when a level is loaded all variables are reset but you can try it out.
Answer by clunk47 · May 24, 2013 at 03:46 PM
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;
Saw you were using static variables when you added the correct script, edited my above answer. Check out DontDestroyOnLoad. static is only used if there is only one instance of something, ever.
when you access something static, it's (scriptname.variable). Ins$$anonymous$$d, when not using static, use GetComponent. I'll edit my answer again.
Sorry for maybe stupid questions but what am I gonna use this script for? var testVar : int = 0; var scriptObject : GameObject; scriptObject.GetComponent(StaticReset).testVar = 100;
Isn't it any simpler way to do it? As I said to "ExTheSea", when I'm removing static I get errors in the script.
You can of course do it using static variables but you have to be aware that you might get into trouble later since they can only exist one instance per class/script. This might work now but it's possible that you're digging a hole you later can't get out easily.
It's always best to avoid using static variables if possible.