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
1
Question by L1vD3v · Nov 09, 2014 at 01:59 AM · javascripttimesave

Save Timer HighScore

I have a script, which has a timer and i want to make a highscore system for that timer. I have already made a script but it won't save the highscore. It only updates the score, so if the score is 45 seconds, the best will be 45 seconds and then if it is 65 seconds the next time, it will update to 65 seconds. But it isn't supposed to, because 65 seconds is slower than 45 seconds. Anyone who knows what is wrong?

Here is the script.

 var bestTime : int = 0;
 var completeTime : int = 0;
 
 static var gameTimeAllowed : float = 60.0;
 static var gameTimeRemaining : float = gameTimeAllowed;
 
 function Start() {
 
  bestTime = PlayerPrefs.GetInt("bestTime",bestTime);
 
 }
 
 function Update() { 
    
        completeTime = gameTimeAllowed - gameTimeRemaining;
    
       if(bestTime < completeTime){
       bestTime = completeTime;
       PlayerPrefs.SetInt("bestTime",bestTime);
     }
     
 }
 
 function OnGUI() {
 
 
          GUI.Label(Rect(Screen.width/2-80,Screen.height/2-130,300,100),"Best Time : " +  bestTime.ToString() + " Seconds");
 
 }
 
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
2
Best Answer

Answer by MrSoad · Nov 09, 2014 at 02:04 AM

Change :

 if(bestTime < completeTime){

to :

 if (bestTime > completeTime) {

Your evaluation is the wrong way round, so whenever completeTime is more than the best time you are setting it to be the best time at the moment.

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 L1vD3v · Nov 09, 2014 at 12:14 PM 1
Share

I appreciate you help, but now it just keeps saying the best score is 0?

avatar image MrSoad · Nov 09, 2014 at 12:22 PM 0
Share

If you give it a starting value of 0 then you have given it the best score you can ever get right from the start. Give it your maximum allowed time for the level as it's starting value.

 var bestTime : int = 400;

You will need to delete your current player pref for bestTime as well, or it will continue to override your setting with an unbeatable 0.

avatar image L1vD3v · Nov 09, 2014 at 12:44 PM 1
Share

Thank you so much, you just saved my day.

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

27 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

Related Questions

How to disable animation.Play? (FPS) 1 Answer

How to measure the real time differential between application runs 0 Answers

facebook with unity problem 0 Answers

Not loading an object if it have be retained from a previous scene. 1 Answer

How to save a scene while playing? 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