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 Ssiroo · Sep 11, 2014 at 05:43 PM · playerprefstimersetfloat

HighScore - Timer

Hello, Im trying to make a level completion timer for my game. What Im trying to do is make a string show the time of the level completion, but, the problem is that saving a high score isn't working. It keeps being 00:00:00. I want the shortest time to be the high score.

This is my whole script :

 var highScore : String;
 var score : String;
 
 var guiSkin : GUISkin;
 
 var minutes : float;
 var seconds : float;
 var miliseconds : float;
 
 var levelComplete : boolean = false;
 
 function Awake()
 {
     
 }
 
 function Update()
 {
 
     score = minutes.ToString("00") + ":" + seconds.ToString("00") + ":" + miliseconds.ToString("100");
     highScore = PlayerPrefs.GetFloat("Minutes").ToString("00") + ":" + PlayerPrefs.GetFloat("Seconds").ToString("00") + ":" + PlayerPrefs.GetFloat("Miliseconds").ToString("00");
     PlayerPrefs.SetString("HighScore", highScore);
     miliseconds += 60 * Time.deltaTime;
 
     if(miliseconds > 60)
     {
         miliseconds = 0;
         seconds += 1;
     }
     
     if(seconds > 60)
     {
         minutes += 1;
         sesconds = 0;
     }
 }
     
 function OnGUI()
 {
     if(levelComplete == true)
     {
         if(seconds < PlayerPrefs.GetFloat("Seconds"))
         {
             PlayerPrefs.SetFloat("Seconds", seconds);
         }
 
         if(miliseconds < PlayerPrefs.GetFloat("Miliseconds"))
         {
             PlayerPrefs.SetFloat("Miliseconds", miliseconds);
         }
 
         if(minutes < PlayerPrefs.GetFloat("Minutes"))
         {
             PlayerPrefs.SetFloat("Minutes",minutes);
         }
     }
 }


Thank you for your time.

Edit :

I forgot to add another script of mine, this one:

 var scoreBoard : ScoreBoard;
 
 function Awake()
 {
     scoreBoard = GameObject.FindWithTag("Player").GetComponent(ScoreBoard);
 }
 
 function OnCollisionEnter (col : Collision)
 {
     if(col.collider.gameObject.tag == "Player")
     {
         scoreBoard.levelComplete = true;
         Time.timeScale = 0;
     }
 }

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 Ssiroo · Sep 12, 2014 at 11:25 AM 0
Share

Bump. I really need this!

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by KMKxJOEY1 · Sep 11, 2014 at 06:32 PM

Setting playerprefs every frame is not efficient, I would have a game over event based method that will update the high score if it is smaller like so:

 var score : String;
  
 var guiSkin : GUISkin;
  
 var minutes : float;
 var seconds : float;
 var miliseconds : float;
  
 var levelComplete : boolean = false;
 var gameOver : boolean = false;
  
 function Update()
 {
     score = minutes.ToString("00") + ":" + seconds.ToString("00") + ":" + miliseconds.ToString("100");
     
     miliseconds += 60 * Time.deltaTime;
  
     if(miliseconds > 60)
     {
         miliseconds = 0;
         seconds += 1;
     }
  
     if(seconds > 60)
     {
         minutes += 1;
         sesconds = 0;
     }
     
     if(levelComplete && !gameOver)
     {
         GameOver();
     }
 }
  
 function GameOver()
 {
     gameOver = true;
     
     int timeValue = minutes * 100 + seconds * 10 + miliseconds;
     if(PlayerPrefs.GetInt("highTimeValue", 0) > timeValue)
     {
         PlayerPrefs.SetInt("highTimeValue", timeValue);
         PlayerPrefs.SetFloat("Minutes",minutes);
         PlayerPrefs.SetFloat("Seconds", seconds);
         PlayerPrefs.SetFloat("Miliseconds", miliseconds);
         highScore = PlayerPrefs.GetFloat("Minutes").ToString("00") + ":" + PlayerPrefs.GetFloat("Seconds").ToString("00") + ":" + PlayerPrefs.GetFloat("Miliseconds").ToString("00");
         PlayerPrefs.SetString("HighScore", highScore);
     }
 }

Also, I fixed your time logic (you were updating each time interval differently. So lets say game 1 I got 1 minute 2 seconds and game 2 I got 2 minutes 1 second, you were writing over the seconds because that value was lower. Take a look at my code that determines the time value above. Also you spelled milliseconds wrong, but didn't change that for you :p

Comment
Add comment · Show 1 · 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 Ssiroo · Sep 11, 2014 at 06:56 PM 0
Share

It's still stuck at 00:00:00. I tried making a function where you press T to call the GameOver function and test if your script is working but it's just not...I also made a Gui.textfield that uses PlayerPrefs.GetString("HighScore") for that. $$anonymous$$aybe im making a mistake or something but Im really stuck in here. Thank you for your quick response btw.

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

24 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

Related Questions

Why cant I change playerprefs value? 1 Answer

Unity Countdown timer in C# 1 Answer

Save a timer in Hour:Minutes:Seconds to playerPrefs then load it on start 2 Answers

PlayerPrefs saving, but not loading correctly 1 Answer

Timer highscore trouble 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