using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class SaveHighscore : MonoBehaviour
{
public Text highscoreT;
void Start()
{
highscoreT = GetComponent<Text>();
highscoreT.text = PlayerPrefs.GetFloat("Highscore", 0).ToString();
}
public static void UpdateScore()
{
if(Timer.time < PlayerPrefs.GetFloat("Highscore", Timer.time))
{
PlayerPrefs.SetFloat("Highscore", Timer.time);
}
}
}
In my level scene, I have goal with a script attached, and when you reach the goal, it calls the UpdateScore() function.
How can I make it so, that I don’t see my highscore time as a float, but like in this picture?
Uses the FromSeconds method GroZZler linked, and format string found here:
It’s pretty straight forward, you should read through both pages to get an understanding.
There are lots of formatting strings for numbers, timespan, datetime, and more.
Note this is not actually a “unity” thing, it’s a .net/mono/C# thing. And you can google around the internet in those context (rather than unity) for lots of help on it. C# is going on 20 years old, and was created by Microsoft one of the largest tech companies, there’s LOTS of info about it.
I am trying everything I can, but nothing works, I am getting alot of errors with everything I try. As a beginner in programming and game developer, this all is so confusing for me…
If I put my “Timer.time” into the (timeInSeconds), it gives me the error: “Input string was not in the correct format”. I can’t find the solution to that error on the internet. Does anyone know how I can fix it…
(I am working on this highscore system for 3 days now and I am getting more confused each day because I can’t get it done, how hard I try :()
Sorry, I’ve been in work mode all week at my day job… I’m in newest version of .net mode. Lots of OT as it’s crunch time.
I forgot that Unity uses an older version and that the ToString(string format) overload of ToString for TimeSpan doesn’t exist.
Instead you have to explicitly format the stupid thing (I bet if you put it in .net 4.x support mode my original post would work).
Here’s how you have to do it in .net 2.0 support mode:
var ts = TimeSpan.FromSeconds(timeInSeconds);
highscoreT.text = string.Format("{0:00}:{1:00}", ts.Minutes, ts.Seconds);
or
var ts = TimeSpan.FromSeconds(timeInSeconds);
highscoreT.text = string.Format("{0:00}:{1:00}", ts.TotalMinutes, ts.Seconds);
If you expect the minutes to go over 59 and you want to show them in long minute form. If you wanted it to show hours as well, well you’d expand on your formatting from there to get it.
Note difference between Minutes and TotalMinutes (Seconds and TotalSeconds) is one is the minutes portion of the time, the other is the total minutes of the entire time.
Think of it like 3 hours 45 minutes… Minutes is 45, but TotalMinutes is 225 (the 3 hours of 60 minutes each plus 45)
This is a rather trivial math + string operation to just do manually without use of TimeSpan or string.Format. Doing it this way may be more comfortable for some people.
int timeInSecondsInt = (int)timeInSeconds; //We don't care about fractions of a second, so easy to drop them by just converting to an int
int minutes = timeInSeconds / 60; //Get total minutes
int seconds = timeInSecondsInt - (minutes * 60); //Get seconds for display alongside minutes
highscoreT.text = minutes.ToString("D2") + ":" + seconds.ToString("D2"); //Create the string representation, where both seconds and minutes are at minimum 2 digits
This solution worked for me, but I noticed Unity was rounding my minutes up, until the actual timer ran down to below 30 seconds. so, I added the (int) cast:
Beware that TimeSpan.ToString generates a lot more garbage than calculating the time strings manually.
I just benchmarked it on device (albeit in a development build, not sure how that affects this) and I get about 10x more garbage using TimeSpan.ToString on Android ARM 64. This might not be a problem for you but we are using a milliseconds display updated each frame which TimeSpan.ToString generates 0.5kb of garbage to display versus 50B for the manual method.
This can be quite unbearable depending on your target platform. Then again there are ways of pre-allocating a lot of stuff to get this to work with 0 garbage if it’s a big issue and worth the investment.