Search Unity

Convert float to time (Minutes and seconds)

Discussion in 'Scripting' started by Laurenz_02, May 11, 2019.

  1. Laurenz_02

    Laurenz_02

    Joined:
    Oct 21, 2017
    Posts:
    52
    Hey, I have a game, in which you have to set the fastest time during some levels.

    Right now, my highscore time is being displayed like this:

    upload_2019-5-11_17-9-46.png

    The script for this is:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class SaveHighscore : MonoBehaviour
    7. {
    8.     public Text highscoreT;
    9.  
    10.     void Start()
    11.     {
    12.         highscoreT = GetComponent<Text>();
    13.         highscoreT.text = PlayerPrefs.GetFloat("Highscore", 0).ToString();
    14.     }
    15.  
    16.     public static void UpdateScore()
    17.     {
    18.         if(Timer.time < PlayerPrefs.GetFloat("Highscore", Timer.time))
    19.         {
    20.             PlayerPrefs.SetFloat("Highscore", Timer.time);
    21.         }
    22.     }
    23. }
    24.  
    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?

    upload_2019-5-11_17-12-11.png

    Thanks in advance!
     

    Attached Files:

  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    2,147
  3. Laurenz_02

    Laurenz_02

    Joined:
    Oct 21, 2017
    Posts:
    52
    Okay, thanks for your reply.

    I don't have that much experience in unity, as I am a beginner.

    Could you make an example for me with my highscore script?

    That would be really appreciated!

    Thanks in advance!
     
  4. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    6,467
    Code (csharp):
    1. highscoreT.text = TimeSpan.FromSeconds(timeInSeconds).ToString("mm:ss");
    Uses the FromSeconds method GroZZler linked, and format string found here:
    https://docs.microsoft.com/en-us/dotnet/standard/base-types/custom-timespan-format-strings

    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.
     
    Last edited: May 11, 2019
    Ryiah and Laurenz_02 like this.
  5. Laurenz_02

    Laurenz_02

    Joined:
    Oct 21, 2017
    Posts:
    52
    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....

    highscoreT.text = TimeSpan.FromSeconds(timeInSeconds).ToString("mm:ss");

    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 :()

    Can anyone PLEASE make my code work....
     
  6. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    3,620
    Are you putting this with quotes? If yes, have your tried without quotes?
     
    Laurenz_02 likes this.
  7. Laurenz_02

    Laurenz_02

    Joined:
    Oct 21, 2017
    Posts:
    52
    No, I am not putting it with quotes.

    Thanks for you reply anyway :)
     
  8. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    3,620
    Laurenz_02 likes this.
  9. Laurenz_02

    Laurenz_02

    Joined:
    Oct 21, 2017
    Posts:
    52
    Then its saying: Unrecognized escape sequence.
     
  10. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    6,467
    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:
    Code (csharp):
    1. var ts = TimeSpan.FromSeconds(timeInSeconds);
    2. highscoreT.text = string.Format("{0:00}:{1:00}", ts.Minutes, ts.Seconds);
    or

    Code (csharp):
    1. var ts = TimeSpan.FromSeconds(timeInSeconds);
    2. 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)
     
    Ryiah and Laurenz_02 like this.
  11. Laurenz_02

    Laurenz_02

    Joined:
    Oct 21, 2017
    Posts:
    52
    Dude,

    Thank you SO MUCH!!

    You are now officially declared as hero to me :)

    I got so frustrated that it didn't work, that I almost wanted to give up.

    THANKS FOR REPLYING AND HELPING!!
     
  12. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    3,620
    Man, I hate that setting. I've missed the possibility above as well. :D
     
    Ryiah, Laurenz_02 and lordofduct like this.