So I made a game where you have to get points and if you have them all you win, I added a stopwatch and a highscore to it so you could “speedrun” the level, but the highscore won’t save.
int seconds = PlayerPrefs.GetInt("HighScore", 0);
bestTimeText.text = PlayerPrefs.GetInt("HighScore", 0).ToString();
currentTime = 0;
stopwatchActive = true;
}
// Update is called once per frame
public void Update()
{
if(stopwatchActive == true)
{
currentTime = currentTime + Time.deltaTime;
}
TimeSpan time = TimeSpan.FromSeconds(currentTime);
currentTimeText.text = time.ToString(@"mm\:ss\:fff");
TimeSpan highTime = TimeSpan.FromSeconds(bestTime);
bestTimeText.text = highTime.ToString(@"mm\:ss\:fff");
PlayerPrefs.SetInt("HighScore", (int)time.TotalSeconds);
}
public void SetTime()
{
TimeSpan time = TimeSpan.FromSeconds(currentTime);
currentTimeText.text = time.ToString(@"mm\:ss\:fff");
TimeSpan highTime = TimeSpan.FromSeconds(bestTime);
bestTimeText.text = highTime.ToString(@"mm\:ss\:fff");
if (time < highTime)
{
print("Highscore!");
bestTimeText.text = time.ToString(@"mm\:ss\:fff");
PlayerPrefs.SetInt("HighScore", (int)time.TotalSeconds);
PlayerPrefs.Save();
}
}
public void StartStopwatch()
{
stopwatchActive = true;
}
public void StopStopwatch()
{
stopwatchActive = false;
}
public void Reset()
{
bestTime = 600;
}