- Home /
I'm trying to get and store max elapsed time in min:sec format. Also, the best time for Highscore. Not working!
After trying out many different approaches, the following two options seems to get the closest to work out:-
The other incomplete approach is commented out in the script
public TextMeshProUGUI timeCounterText; public TextMeshProUGUI maxTimeText;
/ private float maxTimeSeconds; private float maxTimeMinutes; ``private float runTimeMin; private float runTimeSec; / private float startTime; private float runTime; private float maxTime; private float gameplayTime;
void Start() { gameRunTime = true;
startTime = Time.timeSinceLevelLoad;
/* maxTimeMinutes = PlayerPrefs.GetFloat("Max Time in Mins", maxTimeMinutes);
maxTimeSeconds = PlayerPrefs.GetFloat("Secs in max time", maxTimeSeconds); */
maxTime = PlayerPrefs.GetFloat("Max Time", maxTime);
void Update() { if (gameRunTime == true) { gameplayTime += Time.smoothDeltaTime * 10; }
int seconds = (int)(gameplayTime % 60);
int minutes = (int)(gameplayTime / 60f) % 60;
timeCounterText.text = minutes.ToString("00") + ":" + seconds.ToString("00");
/* float runTimeSec = (float)seconds;
float runTimeMin = (float)minutes;
UpdateMaxMins();
UpdateMaxSec();
maxTimeText.text = maxTimeMinutes.ToString("00") + ":" + maxTimeSeconds.ToString("00"); */
runTime = gameplayTime - startTime;
if (runTime > maxTime)
{
maxTime = runTime;
PlayerPrefs.SetFloat("Max Time", maxTime);
PlayerPrefs.SetFloat("", runTime);
}
maxTimeText.text = maxTime.ToString("00:00");
/* private void UpdateMaxMins() { if (runTimeMin > maxTimeMinutes) { maxTimeMinutes = runTimeMin; PlayerPrefs.SetFloat("Max Time in Mins", maxTimeMinutes); } }
private void UpdateMaxSec()
{
if ((runTimeMin > maxTimeMinutes) )
{
if ((gameRunTime == false))
{
maxTimeSeconds = runTimeSec;
PlayerPrefs.SetFloat("Secs in max time", maxTimeSeconds );
}
*/
public void GameOver() { gameOverText.text = "GAME OVER"; gameOver = true;
if (gameOver)
{
gameRunTime = false;
}
}[`link text`][1]
[1]: /storage/temp/108574-new-text-document.txt
The above script works except for maxTime keeps counting till 100 (instead of 59) and then adds a 1 minute and starts counting seconds again. I need it to count till 59 for seconds and add 1 min and resumes counting seconds and so on, just like timeCounter. And the commented out script, it simply stops functioning as I store runTimesec to maxTimeSeconds, as in no outputs.
I'm new to unity and C#. Stuck here for a day and half :( I'd really appreciate any suggestions or solutions. Thanks in advance!
Is there any reason you are using playerprefs in your code? Why not just use it to store the high score for time?
I am using it to store maxTime (highscore for time).
If you want a $$anonymous$$ute counter try this:
private float time;
private int $$anonymous$$utes;
void Update(){
time += time.deltaTime;
if(time >= 60)
{
$$anonymous$$utes += 1;
time = 0;
}
yourText.text = $$anonymous$$utes.ToString() + ":" + (int)time.ToString();
}
//if you want to store high scores
private void HighScore()
{
float totalTime = ($$anonymous$$utes * 60)+ time;
if(PlayerPrefs.GetFloat("highestTime") < totalTime){
PlayerPrefs.SetFloat("highestTime", totalTime);
}
}
Answer by Buretto · Jan 05, 2018 at 06:17 AM
If you want a minute counter try this:
private float time;
private int minutes;
void Update(){
time += time.deltaTime;
if(time >= 60)
{
minutes += 1;
time = 0;
}
yourText.text = minutes.ToString() + ":" + (int)time.ToString();
}
//if you want to store high scores
private void HighScore()
{
float totalTime = (minutes * 60)+ time;
if(PlayerPrefs.GetFloat("highestTime") < totalTime){
PlayerPrefs.SetFloat("highestTime", totalTime);
}
}
Answer by Ensavazor · Jan 05, 2018 at 05:33 PM
@Sandra-Burgle Thanks a lot for getting back to me on my question, really appreciate it!
I just commented out my current script and ran yours. Unfortunately, it seems yours gets me the same output as my current (see following snippet) script mentioned above, is getting me.
private float startTime;
private float runTime;
private float maxTime;
private float gameplayTime;
void Start()
{
gameRunTime = true;
startTime = Time.timeSinceLevelLoad;
maxTime = PlayerPrefs.GetFloat("Max Time", maxTime);
}
if (gameRunTime == true)
{
gameplayTime += Time.smoothDeltaTime * 10;
}
void Update()
{
int seconds = (int)(gameplayTime % 60);
int minutes = (int)(gameplayTime / 60f) % 60;
timeCounterText.text = minutes.ToString("00") + ":" + seconds.ToString("00");
runTime = gameplayTime - startTime;
if (runTime > maxTime)
{
maxTime = runTime;
PlayerPrefs.SetFloat("Max Time", maxTime);
}
maxTimeText.text = maxTime.ToString("00:00");
}
But, yours is definitely done much simpler and lesser codes and enlightened me with new possible options which I'll try explore and see if it gets me where I aiming to. Thanks again!! ^_^
Your answer
Follow this Question
Related Questions
My high time score isn't working 1 Answer
CountDown Timer Help (Seconds problem) 2 Answers
How do you save timer with PlayerPrefs? 1 Answer
Call function only if last call was >= X seconds ago 1 Answer
Subtract milliseconds from 12 hours? 1 Answer