- Home /
Save best time
Hi!
I have a problem in my script. This is my script. The problem is that the best time doesn't save.
static var currentScore : int = 0;
var timer : float = 0.00;
var bestTime : float;
function Start () {
}
function Update () {
if (currentScore == 20) {
if (bestTime > timer){
bestTime = timer;
var bestTimer = PlayerPrefs.GetFloat("bestTimer");
PlayerPrefs.SetFloat("bestTimer", bestTimer);
PlayerPrefs.Save();
}
}
}
function OnGUI ()
{
GUI.Box(new Rect(90, 10, 70, 20), "" + timer.ToString (""));
}
Timer script:
var timer : float = 0.00;
static var currentScore : int = 0;
function Update ()
{
timer += Time.deltaTime;
}
function OnGUI ()
{
GUI.Box(new Rect(10, 10, 70, 20), "" + timer.ToString (""));
}
Thank you. :)
Answer by Lo0NuhtiK · Jul 18, 2014 at 09:33 PM
Idk, man, your stuff is just weird lookin today... er somethin.
Try this instead of having two scripts that don't talk to eachother.
static var currentScore : int = 0 ;
private var timer : float = 0f ;
private var bestTime : float = 0f ;
funyuns Start()
{
bestTime = PlayerPrefs.GetFloat("BestTime", Mathf.Infinity) ;
}
funyuns Update()
{
SetTimers() ;
}
funyuns SetTimers()
{
timer += Time.deltaTime ;
if(currentScore == 20)
{
if(timer < bestTime)
{
PlayerPrefs.SetFloat("BestTime", timer) ;
bestTime = timer ;
}
}
}
funyuns OnGUI()
{
GUILayout.Box("Timer : " + timer) ;
GUILayout.Box("Best Time : " + bestTime) ;
}
Timer works great but it writes Best time : bestTime and when I reach score 20 it still writes Best time : bestTime. :/ Do you know why? Thank you for your help.
I had put a couple things in the wrong spot and didn't test it or anything. Just edited the original post, still didn't test it, but now it might work how you want lol ...
Assets/skripte/timer.js(23,16): BCE0051: Operator '<' cannot be used with a left hand side of type 'float' and a right hand side of type 'Object'.
I tried to do private var bestTime : float; but it doesn't work as I should. :)
And in the game it writes Best time : Infinity.
I had to go check on my other PC a few $$anonymous$$utes ago after seeing your comment so I could make a js script and see wtf lol...
I don't use js, and this would've worked fine in c# had I written it in that xD ... I don't remember UnityScript being quite so strict when I first used a few years ago, so idk. [var whatever;] usually suffices for a js post I make lol
I tried it with just using a few big numbers ins$$anonymous$$d of infinity, nope that didn't work either.
Its the variable declaration like you thought.
var timer : float ; //var timer = 0f ; worked also, I checked
var bestTime : float ;
And that should do it.
Yeah, the GUI will show Infinity until you get the first "BestTime" set, (if you don't want it to say infinity, make it say something else ins$$anonymous$$d), and also this needs changed->
if(timer < bestTime)
PlayerPrefs...whatever...
...to->
if(timer < bestTime)
{
PlayerPrefs.SetFloat("BestTime", timer) ;
//and then set bestTime to match so it wont keep updating
//and so your gui will show it
bestTime = timer ;
//but I figured you would have been adding stuff to change levels or do something else at this point
//since you had reached a score to set a best time for...
}
Anyway, pretty sure all I did to make it work was changed the variable declarations to specify : float ... if not, I guess I'll go back upstairs and check that test js script again xD
Edit : (Edited original script post, now "maybe" it'll do wtf its supposed to lol)
It doesn't :(. I'm gonna' cry :). I don't know... I tried to change something with my noob knowledge but it doesn't save and bestTime is always Infinity. It doesn't change. I'd try to help you but i don't know.
Thank you anyway.