- Home /
HighScore - Timer
Hello, Im trying to make a level completion timer for my game. What Im trying to do is make a string show the time of the level completion, but, the problem is that saving a high score isn't working. It keeps being 00:00:00. I want the shortest time to be the high score.
This is my whole script :
var highScore : String;
var score : String;
var guiSkin : GUISkin;
var minutes : float;
var seconds : float;
var miliseconds : float;
var levelComplete : boolean = false;
function Awake()
{
}
function Update()
{
score = minutes.ToString("00") + ":" + seconds.ToString("00") + ":" + miliseconds.ToString("100");
highScore = PlayerPrefs.GetFloat("Minutes").ToString("00") + ":" + PlayerPrefs.GetFloat("Seconds").ToString("00") + ":" + PlayerPrefs.GetFloat("Miliseconds").ToString("00");
PlayerPrefs.SetString("HighScore", highScore);
miliseconds += 60 * Time.deltaTime;
if(miliseconds > 60)
{
miliseconds = 0;
seconds += 1;
}
if(seconds > 60)
{
minutes += 1;
sesconds = 0;
}
}
function OnGUI()
{
if(levelComplete == true)
{
if(seconds < PlayerPrefs.GetFloat("Seconds"))
{
PlayerPrefs.SetFloat("Seconds", seconds);
}
if(miliseconds < PlayerPrefs.GetFloat("Miliseconds"))
{
PlayerPrefs.SetFloat("Miliseconds", miliseconds);
}
if(minutes < PlayerPrefs.GetFloat("Minutes"))
{
PlayerPrefs.SetFloat("Minutes",minutes);
}
}
}
Thank you for your time.
Edit :
I forgot to add another script of mine, this one:
var scoreBoard : ScoreBoard;
function Awake()
{
scoreBoard = GameObject.FindWithTag("Player").GetComponent(ScoreBoard);
}
function OnCollisionEnter (col : Collision)
{
if(col.collider.gameObject.tag == "Player")
{
scoreBoard.levelComplete = true;
Time.timeScale = 0;
}
}
Answer by KMKxJOEY1 · Sep 11, 2014 at 06:32 PM
Setting playerprefs every frame is not efficient, I would have a game over event based method that will update the high score if it is smaller like so:
var score : String;
var guiSkin : GUISkin;
var minutes : float;
var seconds : float;
var miliseconds : float;
var levelComplete : boolean = false;
var gameOver : boolean = false;
function Update()
{
score = minutes.ToString("00") + ":" + seconds.ToString("00") + ":" + miliseconds.ToString("100");
miliseconds += 60 * Time.deltaTime;
if(miliseconds > 60)
{
miliseconds = 0;
seconds += 1;
}
if(seconds > 60)
{
minutes += 1;
sesconds = 0;
}
if(levelComplete && !gameOver)
{
GameOver();
}
}
function GameOver()
{
gameOver = true;
int timeValue = minutes * 100 + seconds * 10 + miliseconds;
if(PlayerPrefs.GetInt("highTimeValue", 0) > timeValue)
{
PlayerPrefs.SetInt("highTimeValue", timeValue);
PlayerPrefs.SetFloat("Minutes",minutes);
PlayerPrefs.SetFloat("Seconds", seconds);
PlayerPrefs.SetFloat("Miliseconds", miliseconds);
highScore = PlayerPrefs.GetFloat("Minutes").ToString("00") + ":" + PlayerPrefs.GetFloat("Seconds").ToString("00") + ":" + PlayerPrefs.GetFloat("Miliseconds").ToString("00");
PlayerPrefs.SetString("HighScore", highScore);
}
}
Also, I fixed your time logic (you were updating each time interval differently. So lets say game 1 I got 1 minute 2 seconds and game 2 I got 2 minutes 1 second, you were writing over the seconds because that value was lower. Take a look at my code that determines the time value above. Also you spelled milliseconds wrong, but didn't change that for you :p
It's still stuck at 00:00:00. I tried making a function where you press T to call the GameOver function and test if your script is working but it's just not...I also made a Gui.textfield that uses PlayerPrefs.GetString("HighScore") for that. $$anonymous$$aybe im making a mistake or something but Im really stuck in here. Thank you for your quick response btw.