- Home /
Timer highscore trouble
Hello. I've been trying to have a time based high score in my game and so far I've been able to save my time when finishing a level and displaying it using player.prefs. The problem I'm facing now is that I don't know how to save a high score that would replace the current time that I already have on a level. For example, when a player finishes a level it would compare the new time to the previous time and if it's better it will save the new one if it's worse it will discard it. Here's the script I'm using for my timer
public class StopWatch : MonoBehaviour
{
public float timeStart;
public Text textBox;
public float seconds, minutes, milliseconds, hours;
bool timerActive = true;
// Use this for initialization
void Start()
{
textBox.text = timeStart.ToString("F2");
}
// Update is called once per frame
void Update()
{
if (timerActive)
{
hours = (int)(Time.timeSinceLevelLoad / 3600f);
milliseconds = (int)(Time.timeSinceLevelLoad * 1000f) % 1000;
minutes = (int)(Time.timeSinceLevelLoad / 60f) % 60;
seconds = (int)(Time.time % 60f);
textBox.text = hours.ToString("00") + ":" + minutes.ToString("00") + ":" + seconds.ToString("00") + ":" + milliseconds.ToString("00");
}
}
and here is the code I'm using to save the time to player.prefs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class SaveTime : MonoBehaviour
{
public Text Time;
string _currenttime;
public void SetTime()
{
_currenttime = Time.text;
PlayerPrefs.SetString("Time", _currenttime);
Debug.Log ("Your Time Is " + PlayerPrefs.GetString("Time"));
}
}
If anyone could help me or guide me in the right direction I would greatly appreciate it.
Answer by n_rusev · Apr 05, 2020 at 06:27 AM
Hello. Since you already have your time in seconds with Time.timeSinceLevelLoad, why not just save it like a float and compare later?
You can do:
float timeInSeconds = Time.timeSinceLevelLoad;
if(timeInSeconds < PlayerPrefs.GetFloat("Time", 9999999f) )
{
PlayerPrefs.SetFloat("Time", timeInSeconds);
}
The second parameter to PlayerPrefs.GetFloat is the default value that will be returned, if nothing is saved under "Time". I've made very big number, so that your current time will always be smaller than it. And ofcourse if something is returned and your timeInSeconds is again smaller than it, it will be overwritten.
Hello! thank you for the reply. That seems to work wonderfully the only question left is how would I go about displaying the timeinseconds float onto a text? I tried converting it into a string but that didn't seem to work
Hello, why does converting not work? How did you do it? What error do you get?
Actually a simple timeInSeconds.ToString() should work.
Hey, I'm doing this to try and convert it and display it onto a textbox
timeInSeconds.ToString(_timeinstring);
PlayerPrefs.SetString("TimeInString", _timeinstring);
_stringtext.text = _timeinstring;
Your answer
![](https://koobas.hobune.stream/wayback/20220612230535im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How do I make a Highscore for my timer? 1 Answer
"Best Time"/high score help… 1 Answer
How can I save highscores and display them for later? 1 Answer
PlayerPref set int and get int 1 Answer