• 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
Question by Ensavazor · Jan 04, 2018 at 07:36 PM · timetimerhighscoresecondsmaximum

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!

new-text-document.txt (1.8 kB)
Comment

People who like this

0 Show 3
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 Buretto · Jan 05, 2018 at 05:39 AM 0
Share

Is there any reason you are using playerprefs in your code? Why not just use it to store the high score for time?

avatar image Ensavazor Buretto · Jan 06, 2018 at 06:24 AM 0
Share

I am using it to store maxTime (highscore for time).

avatar image Buretto · Jan 05, 2018 at 05:52 AM 0
Share

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


2 Replies

· Add your reply
  • Sort: 
avatar image

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

Comment
Ensavazor

People who like this

1 Show 0 · 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

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!! ^_^

Comment

People who like this

0 Show 0 · 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

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.

Update about the future of Unity Answers

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta later in June. Please note, we are aiming to set Unity Answers to read-only mode on the 31st of May in order to prepare for the final data migration.

For more information, please read our full announcement.

Follow this Question

Answers Answers and Comments

74 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

Related Questions

CountDown Timer Help (Seconds problem) 2 Answers

How do you save timer with PlayerPrefs? 1 Answer

My high time score isn't working 1 Answer

Call function only if last call was >= X seconds ago 1 Answer

How to change the colour of a light every few seconds. 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