- Home /
Saving Times as Highscore
Hello,
I started making a small game 1 Week ago, but now I am stuck. The Highscore sets now on any time if I have timer.time > highScore
. But if I use timer.time < highScore
it doesnt save any Highscore. I also tried to get the float from the Playerprefs, that doesnt work also. The code below is the latest. It may be a really stupid mistake somewhere, like a writing mistake.
I thank in advance,
-Cheatsrichter
FULL CODE:
using UnityEngine;
using UnityEngine.UI;
public class highScoreTracker : MonoBehaviour {
private Timer timer;
public float highScore;
public Text text;
void Start(){
timer = GetComponent<Timer>();
}
void Update(){
Debug.Log(PlayerPrefs.GetFloat(Application.loadedLevel.ToString() + "SCORE"));
if(timer.time < PlayerPrefs.GetFloat(Application.loadedLevel.ToString() + "SCORE")){
highScore = Mathf.Round(timer.time * 100f) / 100f;
Debug.Log("HSS " + highScore);
PlayerPrefs.SetFloat(Application.loadedLevel + "SCORE", highScore);
PlayerPrefs.Save();
}
text.text = "TOP TIME : " + PlayerPrefs.GetFloat(Application.loadedLevel.ToString() + "SCORE").ToString();
}
}
Why would you save the high score if the timer is less than the previous high score ?
By the way, it's not advised at all to make I/O operation in an Update function (I guess GetFloat and SetFloat are respectively reading and writing to a file). This kind of operation consumes resources and can block the thread of Unity if the file is locked for example (for any reason ...)
You should consider getting the last high score in the Start function and writing the new one in the OnDestroy function for example.
Don't forget to post the solution in the case someone has the same problem ! And don't forget to accept your answer just after (check mark on the left of the answer)
But if you fix one, another problem comes: The highscore just is zero all the time, i have no idea...
CODE :
using UnityEngine;
using UnityEngine.UI;
public class highScoreTracker : $$anonymous$$onoBehaviour {
public Timer timer;
public float highScore = 15f;
public Text text;
void Start(){
highScore = PlayerPrefs.GetFloat(Application.loadedLevelName);
if(highScore == 0){
highScore = 15;
}
ChangeText();
}
void Update(){
Debug.Log(highScore);
if(timer.time < highScore){
highScore = $$anonymous$$athf.Round(timer.time * 100) / 100;
Save();
}
}
public void Save(){
PlayerPrefs.SetFloat(Application.loadedLevelName, highScore);
PlayerPrefs.Save();
print("saved");
ChangeText();
}
public void ChangeText(){
text.text = "TOP TI$$anonymous$$E : " + highScore.ToString();
}
}
Answer by Cheatsrichter2 · Jun 13, 2015 at 07:04 PM
Took me hours to find the error, it was not in that script, it was in my Timer script.
The answer :
Final High Score Script:
using UnityEngine;
using UnityEngine.UI;
public class highScoreTracker : MonoBehaviour {
public Timer timer;
public float highScore = 15f;
public Text text;
void Start(){
highScore = PlayerPrefs.GetFloat(Application.loadedLevelName);
if(highScore == 0){
highScore = 60;
text.text = "TOP TIME : NONE";
} else {
ChangeText();
}
}
void Update(){
Debug.Log(highScore);
if(timer.time < highScore){
highScore = Mathf.Round(timer.time * 100) / 100;
Save();
}
}
public void Save(){
PlayerPrefs.SetFloat(Application.loadedLevelName, highScore);
PlayerPrefs.Save();
print("saved");
ChangeText();
}
public void ChangeText(){
text.text = "TOP TIME : " + highScore.ToString();
}
}
The Timer Script :
using UnityEngine;
using UnityEngine.UI;
public class Timer : MonoBehaviour {
public float time = 60;
public float tempTime;
public Text text;
void Update(){
if(!goal.isGoal){
tempTime += Time.deltaTime;
time = 60;
} else {
time = tempTime;
//to scores see highScoreTracker
}
text.text = "TIME : " + tempTime.ToString("F2");
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613200915im_/https://answers.unity.com/themes/thub/images/avi.jpg)