• 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 Cheatsrichter2 · Jun 12, 2015 at 02:32 PM · timeplayerprefshighscore

Saving Times as Highscore

Hello,

I started making a small game 1 Week ago, but now I am stuck. The Highscore sets now on any time if I have timer.time > highScore. But if I use timer.time < highScore it doesnt save any Highscore. I also tried to get the float from the Playerprefs, that doesnt work also. The code below is the latest. It may be a really stupid mistake somewhere, like a writing mistake.

I thank in advance,

-Cheatsrichter

FULL CODE:

 using UnityEngine;
 using UnityEngine.UI;
 
 public class highScoreTracker : MonoBehaviour {
 
     private Timer timer;
 
     public float highScore;
 
     public Text text;
 
     void Start(){
 
         timer = GetComponent<Timer>();
 
     }
 
     void Update(){
 
         Debug.Log(PlayerPrefs.GetFloat(Application.loadedLevel.ToString() + "SCORE"));
 
         if(timer.time < PlayerPrefs.GetFloat(Application.loadedLevel.ToString() + "SCORE")){
 
             highScore = Mathf.Round(timer.time * 100f) / 100f;
 
             Debug.Log("HSS " + highScore);
 
             PlayerPrefs.SetFloat(Application.loadedLevel + "SCORE", highScore);
             PlayerPrefs.Save();
                     
         }
 
         text.text = "TOP TIME : " + PlayerPrefs.GetFloat(Application.loadedLevel.ToString() + "SCORE").ToString();
 
     }
 
 }

Comment
Add comment · Show 4
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 Hellium · Jun 12, 2015 at 02:44 PM 0
Share

Why would you save the high score if the timer is less than the previous high score ?

By the way, it's not advised at all to make I/O operation in an Update function (I guess GetFloat and SetFloat are respectively reading and writing to a file). This kind of operation consumes resources and can block the thread of Unity if the file is locked for example (for any reason ...)

You should consider getting the last high score in the Start function and writing the new one in the OnDestroy function for example.

avatar image Cheatsrichter2 · Jun 12, 2015 at 03:22 PM 0
Share

Well solved it, thanks for the help Hellium

avatar image Hellium · Jun 12, 2015 at 03:33 PM 0
Share

Don't forget to post the solution in the case someone has the same problem ! And don't forget to accept your answer just after (check mark on the left of the answer)

avatar image Cheatsrichter2 · Jun 12, 2015 at 03:56 PM 0
Share

But if you fix one, another problem comes: The highscore just is zero all the time, i have no idea...

CODE :

 using UnityEngine;
 using UnityEngine.UI;
 
 public class highScoreTracker : $$anonymous$$onoBehaviour {
 
     public Timer timer;
 
     public float highScore = 15f;
 
     public Text text;
 
     void Start(){
 
         highScore = PlayerPrefs.GetFloat(Application.loadedLevelName);
 
         if(highScore == 0){
 
             highScore = 15;
 
         }
 
         ChangeText();
 
     }
 
     void Update(){
 
         Debug.Log(highScore);
 
         if(timer.time < highScore){
 
             highScore = $$anonymous$$athf.Round(timer.time * 100) / 100;
 
             Save();
 
         }
 
     }
 
     public void Save(){
 
         PlayerPrefs.SetFloat(Application.loadedLevelName, highScore);
 
         PlayerPrefs.Save();
 
         print("saved");
 
         ChangeText();
 
     }
 
     public void ChangeText(){
 
         text.text = "TOP TI$$anonymous$$E : " + highScore.ToString();
 
     }
 
 }

1 Reply

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

Answer by Cheatsrichter2 · Jun 13, 2015 at 07:04 PM

Took me hours to find the error, it was not in that script, it was in my Timer script.

The answer :

Final High Score Script:

 using UnityEngine;
 using UnityEngine.UI;
 
 public class highScoreTracker : MonoBehaviour {
 
     public Timer timer;
 
     public float highScore = 15f;
 
     public Text text;
 
     void Start(){
 
         highScore = PlayerPrefs.GetFloat(Application.loadedLevelName);
 
         if(highScore == 0){
 
             highScore = 60;
 
             text.text = "TOP TIME : NONE";
 
         } else {
 
             ChangeText();
 
         }
 
     }
 
     void Update(){
 
         Debug.Log(highScore);
 
         if(timer.time < highScore){
 
             highScore = Mathf.Round(timer.time * 100) / 100;
 
             Save();
 
         }
 
     }
 
     public void Save(){
 
         PlayerPrefs.SetFloat(Application.loadedLevelName, highScore);
 
         PlayerPrefs.Save();
 
         print("saved");
 
         ChangeText();
 
     }
 
     public void ChangeText(){
 
         text.text = "TOP TIME : " + highScore.ToString();
 
     }
 
 }

The Timer Script :

 using UnityEngine;
 using UnityEngine.UI;
 
 public class Timer : MonoBehaviour {
 
     public float time = 60;
 
     public float tempTime;
 
     public Text text;
 
     void Update(){
 
         if(!goal.isGoal){
 
             tempTime += Time.deltaTime;
 
             time = 60;
 
         } else {
 
             time = tempTime;
 
             //to scores see highScoreTracker
 
         }
 
         text.text = "TIME : " + tempTime.ToString("F2");
 
     }
     
 }

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

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

How do you save timer with PlayerPrefs? 1 Answer

Making a PlayerPrefs Highscore 0 Answers

How do i make a script that re-fills lives after 10min? 2 Answers

How can a Save a HighScore 2 Answers

Highscore PlayerPrefs not working on iOS 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges