- Home /
How do I make a Highscore for my timer?
I am making a game like Doodle Jump and the aim of the game is to get to the top as fast as possible. I have a working timer script which shows a text counting up on the screen. I want to make a High Score for this but it is difficult because in order for me to use this "<" or this ">" to make it so the high score is only set when you get the fastest time. The timer has to be an int but it is a string. Please help me, Here is the code :
using UnityEngine; using UnityEngine.UI;
public class UITimer : MonoBehaviour { public Text TimerText; public bool playing; private float Timer; public Text HighScoretext;
private void Start()
{
HighScoretext.text = PlayerPrefs.GetString("HighScore");
}
void Update()
{
if (playing == true)
{
Timer += Time.deltaTime;
int minutes = Mathf.FloorToInt(Timer / 60f);
int seconds = Mathf.FloorToInt(Timer % 60f);
int milliseconds = Mathf.FloorToInt((Timer * 100f) % 100f);
TimerText.text = minutes.ToString("0") + ":" + seconds.ToString("00") + "." + milliseconds.ToString("0");
}
if (TimerText.text < PlayerPrefs.GetString("Highscore", 0));
PlayerPrefs.SetString("HighScore", TimerText.text);
}
}
Answer by Spip5 · Aug 25, 2020 at 04:13 PM
You gave the answer in your own question. Use you Timer (it's a float) to compare with the playerprefs, not the string from your text component, and use PlayerPrefs.GetFloat() and PlayerPrefs.SetFloat()
@Spip5 Im kind of new to program$$anonymous$$g and Unity and I did what you said at the end of the script change timertext.text to Timer and change get and set string to get and set float but it is still not working. Here is the script and please tell me if im missing something or if i have to do something. Here :
using UnityEngine; using UnityEngine.UI;
public class UITimer : $$anonymous$$onoBehaviour { public Text TimerText; public bool playing; public float Timer; public Text HighScoretext;
private void Start()
{
HighScoretext.text = PlayerPrefs.GetString("HighScore");
}
void Update()
{
if (playing == true)
{
Timer += Time.deltaTime;
int $$anonymous$$utes = $$anonymous$$athf.FloorToInt(Timer / 60f);
int seconds = $$anonymous$$athf.FloorToInt(Timer % 60f);
int milliseconds = $$anonymous$$athf.FloorToInt((Timer * 100f) % 100f);
TimerText.text = $$anonymous$$utes.ToString("0") + ":" + seconds.ToString("00") + "." + milliseconds.ToString("0");
}
if (Timer < PlayerPrefs.GetFloat("Highscore", 0))
{
PlayerPrefs.SetFloat("HighScore", Timer);
}
}
}
private void Start()
{
UpdateText(PlayerPrefs.GetFloat("HighScore"), HighScoretext);
}
void Update()
{
if (playing == true)
{
Timer += Time.deltaTime;
UpdateText(Timer, TimerText);
}
if (Timer > PlayerPrefs.GetFloat("Highscore", 0))
{
PlayerPrefs.SetFloat("HighScore", Timer);
}
}
void UpdateText(float t, Text text)
{
int $$anonymous$$utes = $$anonymous$$athf.FloorToInt(t / 60f);
int seconds = $$anonymous$$athf.FloorToInt(t % 60f);
int milliseconds = $$anonymous$$athf.FloorToInt((t * 100f) % 100f);
text.text = $$anonymous$$utes.ToString("0") + ":" + seconds.ToString("00") + "." + milliseconds.ToString("0");
}
Sorry @Hellium but its not working the highscore changes no matter what. The if statement where it says to only change the highscore clearly isnt working as it doesnt matter if the recent time was bigger or smaller than the highscore, it will change the highscore. Please Help. Let me know if im being stupid here or if there is an easy way to change this.