Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by etikaram · Aug 18, 2019 at 10:26 AM · unity5timerhighscoreseconds

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));
         }
     }




Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image sacredgeometry · Aug 18, 2019 at 11:07 AM 0
Share

Thats really overcomplicated I can refactor it for you. One sec

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image sacredgeometry · Aug 18, 2019 at 03:56 PM 0
Share

@etikaram Does this help?

avatar image etikaram sacredgeometry · Aug 18, 2019 at 04:13 PM 1
Share

And no i don't have a pausing system but thanks for adding that too.

avatar image etikaram · Aug 18, 2019 at 04:13 PM 1
Share

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");
         }
     }
avatar image etikaram etikaram · Aug 18, 2019 at 05:01 PM 0
Share

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?

avatar image sacredgeometry etikaram · Aug 18, 2019 at 05:02 PM 0
Share

gameLength.ToString(@"mm\:ss");

That format specifier should work on DateTimes or TimeSpans

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

125 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

I'm trying to get and store max elapsed time in min:sec format. Also, the best time for Highscore. Not working! 2 Answers

How do you save timer with PlayerPrefs? 1 Answer

Sending Variable to Highscore Server results in 0. 0 Answers

Timer's tenths of seconds visible digits issue 3 Answers

How to stop a Countdown Timer? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges