- Home /
Timer script running too fast
Ive been using a simple on/off script with my in game flashlight but now Im trying to implement a battery effect and have the flashlight on a countdown timer while switched on. I've updated my original script but the timer is counting down way too fast and I cant figure why?
var Battery : int = 100;
var Batterytext : GUIText;
function Update ()
{
Batterytext.text = " Flashlight " + Battery + " % ";
if (Input.GetKeyDown("f"))
{
if (light.enabled == true)
light.enabled = false;
else
light.enabled = true;
}
if (light.enabled == true)
{
Battery -= Time.deltaTime;
}
}
I did try another method using yield WaitForSeconds but had similar issues.
The on/off function still works and pauses the countdown (if your quick enough...lol) But it doesn't count down in seconds as Id planned it to.
Any suggestions guys ???
Answer by InfiniBuzz · Jul 11, 2013 at 06:20 PM
HI
try this:
var Battery : int = 100;
var Batterytext : GUIText;
var batterySpeed : float = 0.01f;
function Update ()
{
Batterytext.text = " Flashlight " + Battery + " % ";
if (Input.GetKeyDown("f"))
{
if (light.enabled == true)
light.enabled = false;
else
light.enabled = true;
}
if (light.enabled == true)
{
Battery -= Time.deltaTime * batterySpeed ;
}
}
like this, you can easily edit the time, your battery goes down.
Nope, still running down a super speed (didn't even seem to slow it)
:(
have you tried to set batterySpeed to maybe 0.000001?
and try to make Battery variable a float, not int
Ok..
changing the speed alone didn't do anything, no matter how many decimal places.
Changing from battery from 'int' to a 'float' DID work. I just had to change batterySpeed to 1
The only issue now is that the GUI shows the battery % with 5 decimal places after it (Flashlight 97.54321%) which looks really weird
glad it helped.. you can use the String class functions to format the output.
Batterytext.text = "Flashlight: "+ string.Format("{0:0%}",Battery);
sorry, s in string needs to be uppercase I think
Batterytext.text = "Flashlight: "+ String.Format("{0:0%}",Battery);