- Home /
My high time score isn't working
I have a most time survived scoring system but there is something wrong with my timer. When i reach a new high time it shows like ss:ss instead of mm:ss (like 29:29 instead of 00:29) and things getting more interesting, after i restart the game timer works properly(shows like 00:29). Please help me guys. There is my code:
private void Start()
{
startTime = Time.time;
float gameTimer = Time.time - startTime;
string minutes = Mathf.Floor(gameTimer / 60).ToString("00");
string seconds = (gameTimer % 60).ToString("00");
mostTimeSurvived.text = PlayerPrefs.GetFloat("HighTime").ToString(string.Format("{0:00}:{1:00}", minutes, seconds));
}
public void Finish()
{
finished = true;
float gameTimer = Time.time - startTime;
string minutes = Mathf.Floor(gameTimer / 60).ToString("00");
string seconds = (gameTimer % 60).ToString("00");
if (PlayerPrefs.GetFloat("HighTime") < gameTimer)
{
PlayerPrefs.SetFloat("HighTime", gameTimer);
mostTimeSurvived.text = gameTimer.ToString(string.Format("{0:00}:{1:00}", minutes, seconds));
}
}
Thats really overcomplicated I can refactor it for you. One sec
Answer by sacredgeometry · Aug 18, 2019 at 11:15 AM
You could really simplify your code to something like this.
Without Pausing
private DateTime sessionStartTime;
private TimeSpan gameLength;
void Start()
{
sessionStartTime = DateTime.Now;
}
void Update()
{
gameLength = DateTime.Now - sessionStartTime;
var gameLengthText = gameLength.ToString(@"mm\:ss");
}
NOTE
If you have a pausing mechanism this wouldn't work but there are ways to deal with that too, which would be more manageable.
i.e. just storing a collection of time spans for each (or if memory is a concern taking the sum of the last sessions and the current one) session and then taking the sum of them.
With Pausing
private DateTime sessionStartTime;
private TimeSpan gameLength;
private bool IsPaused = false;
void Start()
{
sessionStartTime = DateTime.Now;
}
void Update()
{
if(!IsPaused)
{
var lastSessionLength = DateTime.Now - sessionStartTime;
gameLength += lastSessionLength;
}
sessionStartTime = DateTime.Now;
if(Input.GetKeyDown(KeyCode.Space)) IsPaused = !IsPaused;
var gameLengthText = gameLength.ToString(@"mm\:ss");
}
Edit: Just noticed that you were wanting to evaluate on every frame so updated the code.
And no i don't have a pausing system but thanks for adding that too.
Thanks a lot man, it worked. I did it like that : private void Start() { startTime = DateTime.Now; mostTimeSurvived.text = PlayerPrefs.GetFloat("HighTime").ToString(); }
public void Update()
{
if (finished)
return;
gameLength = DateTime.Now - startTime;
var gameLengthText = gameLength.ToString(@"mm\:ss");
timerText.text = gameLengthText;
timeSurvived.text = gameLengthText;
}
public void Finish()
{
finished = true;
gameLength = DateTime.Now - startTime;
float gameTimer = (float)gameLength.TotalSeconds;
if (PlayerPrefs.GetFloat("HighTime") < gameTimer)
{
PlayerPrefs.SetFloat("HighTime", gameTimer);
mostTimeSurvived.text = gameTimer.ToString("00");
}
}
The problem is i want all of them showed like mm:ss but i don't know how to do it properly. I tried something like private void Start() { startTime = DateTime.Now; gameLength = DateTime.Now - startTime; string $$anonymous$$utes = gameLength.Total$$anonymous$$inutes.ToString(); string seconds = gameLength.TotalSeconds.ToString(); mostTimeSurvived.text = PlayerPrefs.GetFloat("HighTime").ToString(string.Format("{0:00}:{1:00}", $$anonymous$$utes, seconds)); }
but it didn't work, why?
gameLength.ToString(@"mm\:ss");
That format specifier should work on DateTimes or TimeSpans
Your answer
Follow this Question
Related Questions
Call function only if last call was >= X seconds ago 1 Answer
How to stop a Countdown Timer? 1 Answer
Formats of High Score 1 Answer