Save Timer HighScore

Hi yall, I am currently working on a game were it has a timer go and the best time is the one that can complete the course the quickest. For example, the first run if I get 40 seconds that will be saved as the high score but if the second run I get 30 seconds that will be the new high score. My app currently does the opposite were if I get a higher time that will be the new high score.

Notes: Yes I have tried to switch the sign to less than “(t < PlayerPrefs.GetFloat (“HighScore”, 0))” but the issue then is that no time can beat “0” as a high score.

Source Code C#:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Internal;

public class TImer : MonoBehaviour {

public Text timerText;
public Text highscore;
private float startTime;
private bool finished = false;

// Use this for initialization
void Start () {
	startTime = Time.time;
	highscore.text = PlayerPrefs.GetFloat ("HighScore", 0).ToString();

}

// Update is called once per frame
void Update ()
{
	float t = Time.time - startTime;

	string minutes = ((int)t / 60).ToString ();
	string seconds = (t % 60).ToString ("f2");

	timerText.text = minutes + ":" + seconds;

	if (t > PlayerPrefs.GetFloat ("HighScore", 0)) {

		PlayerPrefs.SetFloat ("HighScore", t);
		highscore.text = t.ToString ();

	} 

}

@cbrownz I spent the past 2 days trying to solve this same issue for my game, I couldn’t find any tutorials that dealt with saving the fastest time but after almost losing my mind, I just finally got it to work :slight_smile:
Incase anyone ever asks a similar question, here is my solution dealing with my code. I called the StopTimer() function in another script that handled the end of the level…

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

{

public Text highScore;

public Text timeScore;

public bool timerActive = true;

public float timeTaken;

public float sceneBestTime = 60f;

private void Start()
{
    highScore.text = PlayerPrefs.GetFloat("HighScore", 0).ToString();

    sceneBestTime = PlayerPrefs.GetFloat("CurrentBestTime", sceneBestTime);
}

private void Update()
{
    if (timerActive)
    {
        timeTaken += Time.deltaTime;

        timeScore.text = timeTaken.ToString();
    }
}

public void StopTimer()
{
    timerActive = false;

    if (timeTaken < sceneBestTime)
    {
        highScore.text = timeTaken.ToString();

        PlayerPrefs.SetFloat("CurrentBestTime", timeTaken);

        PlayerPrefs.SetFloat("HighScore", timeTaken);
    }
}

}

@Cuttlas-U So i took script from this web: c# - Save Timer HighScore - Stack Overflow

I think it is the same guy here, because the nickname looks the same too :smiley: But the problem, is that when i start the game Time counts well, but the problem is that highscore always stays at 0. He doesn’t move at all, even if i wait for more than a minute. Like I said i am stuck with it over a week and can’t find any solution in the internet of how to fix it

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Timer : MonoBehaviour
{
public Text timerText;
public Text highscore;
private float startTime;

public void GameFinished()
{
    float t = Time.time - startTime;
    if (t < PlayerPrefs.GetFloat("HighScore", float.MaxValue))
    {
        PlayerPrefs.SetFloat("HighScore", t);
        highscore.text = t.ToString();
        PlayerPrefs.Save();
    }
}

void Start()
{
    startTime = Time.time;
    highscore.text = PlayerPrefs.GetFloat("HighScore: ", 0).ToString();
}

void Update()
{
    float t = Time.time - startTime;
    string minutes = ((int)t / 60).ToString();
    string seconds = (t % 60).ToString("f2");
    timerText.text = "Your Time: " + minutes + ":" + seconds;
}

}