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 ();
}
}
Answer by Khumo187 · Apr 26, 2020 at 04:03 PM
@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 :) 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);
}
}
}
Answer by hubertasjuzenas · Apr 27, 2020 at 09:41 AM
@Cuttlas-U So i took script from this web: https://stackoverflow.com/questions/45262255/save-timer-highscore-unity-c-sharp
I think it is the same guy here, because the nickname looks the same too :D 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;
}
}
you are doing some thing wrong here ;
this is your code when u save :
PlayerPrefs.SetFloat("HighScore", t);
and this is when u load ;
PlayerPrefs.GetFloat("HighScore: ", 0);
you assign two different keys here ; for saving it is "HighScore" and for loading it is "HighScore: " these two need to be the same so u get the right value from the registery ;
@Cuttlas-U I changed it to PlayerPrefs.GetFloat("HighScore", t); ins$$anonymous$$d of using 0, but nothing happens at all. Still showing same result :(
then problem can be else where , u need to print values etc ...
if u are really in problem like a week or so u can install any desk program then via the telegram app send me a massage to my id : @SajjadAzarpour
so i can connect to your system and solve your issue ,
Your answer
Follow this Question
Related Questions
I make a timing score script in unity 2D, how i make high score of time?? Script is as follow: 1 Answer
Disable Ragdoll #C - Timer before disable. 2 Answers
C# - Need help making an enemy spawn timer 1 Answer
,Why isn't the clock speed being affected by the float "clockSpeed"? 0 Answers
Store Reference to a Serializable Class 0 Answers