Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Flaymes · Aug 25, 2020 at 12:44 PM · timerhighscoreshighscore

How do I make a Highscore for my timer?

I am making a game like Doodle Jump and the aim of the game is to get to the top as fast as possible. I have a working timer script which shows a text counting up on the screen. I want to make a High Score for this but it is difficult because in order for me to use this "<" or this ">" to make it so the high score is only set when you get the fastest time. The timer has to be an int but it is a string. Please help me, Here is the code :

using UnityEngine; using UnityEngine.UI;

public class UITimer : MonoBehaviour { public Text TimerText; public bool playing; private float Timer; public Text HighScoretext;

 private void Start()
 {
     HighScoretext.text = PlayerPrefs.GetString("HighScore");
 }

 void Update()
 {

     if (playing == true)
     {

         Timer += Time.deltaTime;
         int minutes = Mathf.FloorToInt(Timer / 60f);
         int seconds = Mathf.FloorToInt(Timer % 60f);
         int milliseconds = Mathf.FloorToInt((Timer * 100f) % 100f);
         TimerText.text = minutes.ToString("0") + ":" + seconds.ToString("00") + "." + milliseconds.ToString("0");
     }

     if (TimerText.text < PlayerPrefs.GetString("Highscore", 0));
     PlayerPrefs.SetString("HighScore", TimerText.text);
 }

}

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Spip5 · Aug 25, 2020 at 04:13 PM

You gave the answer in your own question. Use you Timer (it's a float) to compare with the playerprefs, not the string from your text component, and use PlayerPrefs.GetFloat() and PlayerPrefs.SetFloat()

Comment
Add comment · Show 3 · 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 Flaymes · Aug 26, 2020 at 03:59 PM 0
Share

@Spip5 Im kind of new to program$$anonymous$$g and Unity and I did what you said at the end of the script change timertext.text to Timer and change get and set string to get and set float but it is still not working. Here is the script and please tell me if im missing something or if i have to do something. Here :

using UnityEngine; using UnityEngine.UI;

public class UITimer : $$anonymous$$onoBehaviour { public Text TimerText; public bool playing; public float Timer; public Text HighScoretext;

 private void Start()
 {
     HighScoretext.text = PlayerPrefs.GetString("HighScore");
 }

 void Update()
 {
     if (playing == true)
     {
         Timer += Time.deltaTime;
         int $$anonymous$$utes = $$anonymous$$athf.FloorToInt(Timer / 60f);
         int seconds = $$anonymous$$athf.FloorToInt(Timer % 60f);
         int milliseconds = $$anonymous$$athf.FloorToInt((Timer * 100f) % 100f);
         TimerText.text = $$anonymous$$utes.ToString("0") + ":" + seconds.ToString("00") + "." + milliseconds.ToString("0");
     }
     if (Timer < PlayerPrefs.GetFloat("Highscore", 0))
     {
         PlayerPrefs.SetFloat("HighScore", Timer);
     }
 }

}

avatar image Hellium Flaymes · Aug 26, 2020 at 04:10 PM 1
Share
  private void Start()
  {
      UpdateText(PlayerPrefs.GetFloat("HighScore"), HighScoretext);
  }
 
  void Update()
  {
      if (playing == true)
      {
          Timer += Time.deltaTime;
          UpdateText(Timer, TimerText);
      }
      if (Timer > PlayerPrefs.GetFloat("Highscore", 0))
      {
          PlayerPrefs.SetFloat("HighScore", Timer);
      }
  }
 
 void UpdateText(float t, Text text)
 {
          int $$anonymous$$utes = $$anonymous$$athf.FloorToInt(t / 60f);
          int seconds = $$anonymous$$athf.FloorToInt(t % 60f);
          int milliseconds = $$anonymous$$athf.FloorToInt((t * 100f) % 100f);
          text.text = $$anonymous$$utes.ToString("0") + ":" + seconds.ToString("00") + "." + milliseconds.ToString("0");
 }

avatar image Flaymes Hellium · Aug 27, 2020 at 01:00 PM 0
Share

Sorry @Hellium but its not working the highscore changes no matter what. The if statement where it says to only change the highscore clearly isnt working as it doesnt matter if the recent time was bigger or smaller than the highscore, it will change the highscore. Please Help. Let me know if im being stupid here or if there is an easy way to change this.

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

135 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Timer highscore trouble 1 Answer

I need some help with my high score.,I need help with my highsore. 1 Answer

How do you save timer with PlayerPrefs? 1 Answer

How can I save highscores and display them for later? 1 Answer

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


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