Can anyone tell me why my PlayerPrefs is not remembering the fastest time I got in my game?
In the main menu, the highscore is supposed to be shown, but when I restart the scene, its saying my highscore is 00:00, while it isn’t.
This is my script for storing the highscore:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class SaveHighscore : MonoBehaviour
{
public Text highscoreT;
void Start()
{
highscoreT = GetComponent<Text>();
var ts = TimeSpan.FromSeconds(Timer.time);
highscoreT.text = string.Format("{0:00}:{1:00}", ts.Minutes, ts.Seconds);
}
public static void UpdateHighscore()
{
if(Timer.time < PlayerPrefs.GetFloat("highScore", Timer.time))
{
PlayerPrefs.SetFloat("highScore", Timer.time);
}
}
}
And in this script I am calling the UpdateHighscore() function:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class Goal : MonoBehaviour
{
void OnTriggerEnter2D(Collider2D coll)
{
if(coll.gameObject.name == "Player")
{
//LevelCount.levelValue += 1;
//SceneManager.LoadScene(LevelCount.levelValue);
SceneManager.LoadScene("zEndScene");
SaveHighscore.UpdateHighscore();
}
}
}
If this is the only means of admitting a new high score, then the input must be less than the prior record, right? If the prior record is 00:00, then only a number lower than that will be accepted (which makes no sense for time). If you mistakenly wrote 00:00 to highScore once already, then that bit of code won’t do anything.
Your default value of the GetFloat is not correct, because if highScore is not set, that if-statement condition will always be false.
Let’s say Timer.time is 5, and you have never saved highScore, so PlayerPrefs.GetFloat("highScore", Timer.time) will also return whatever Timer.time returns, in this case 5. So the if-statement condition would be
if (5 < 5), which is false. Therefore you will never set the float.
I would do something like PlayerPrefs.GetFloat("highScore", float.MaxValue) instead
When I include ‘float.MaxValue’ inside the brackets, it still doesn’t work.
When I finish the level, I see the ending scene which is saying what my time is; then when I click the ‘Go back to main menu button’, I can see my highscore being displayed in the main menu scene, but then, when I leave the game, and play again, its saying the highscore is 00:00
Or do I have to do something else, like declaring a minimumTime variable, and calling that minimumTime whenever the Time the player got is lower then any other previous time?
I think you should learn debug a bit what’s happening exactly.
One way is by sprinkling some Debug.Logs to see what’s happening exactly in your code.
Like if there’s an if-statement and you think it’s being executed, do Debug.Logs. If in the Console View the output of the debug logs isn’t what you expected, you’re a step closer to solve the problem.
Another way is by interactive debugging, where add a breakpoint in the code with Visual Studio (or whatever IDE you’re using), and step through the code to see what’s going on.
Okay, I did some debugging now, but I still can’t solve my problem. Debugging the time I got works perfecly fine, the problem is just something in that if statement, but I don’t know how to solve it.
As I said, maybe its an idea to declare a minimumTime variable, and call this variable whenever the Time I get in game shorter than the current highscore and then display the minimumTime variable using PlayerPrefs.
@Lorenz_02, I had already given you a solution by using a getter/setter in your other forum post:
From all your questions you’re spawning on the same topic, it seems that you need to change your frame of mind, and start with some basic programming principles first. You need to first learn how to walk then run.
Also you need to approach debugging like you are solving a puzzle. If through debugging you haven’t realised what the problem is, then you haven’t finished debugging. Debugging = removing of a bug with investigation. When you’re debugging you will always have that “AHA!” moment. If you haven’t had that moment yet then, you need to continue digging deeper.
You can take this simple problem you have as an opportunity to learn how to tackle bugs.
What did you do to debug it?
You need see why that if statement is not working. What’s the value of Timer.time and the playerprefs value before the if statement? Just do a Debug.Log with the values you want to see before the if statement.
(P.S. I can easily tell you to send me your code and debug it for you, but you wouldn’t have gained anything really, because on the next problem you will bombard these forums with your next problem).