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 Ful · Jan 26, 2014 at 06:28 PM · timeplayerprefstimerhighscore

How do you save timer with PlayerPrefs?

Hello Everyone,

I'm creating a survival game where a timer displays how long the player is currently surviving. The timer is working perfectly however I'm having some difficulties on saving it whenever the player gets destroyed and survives the longest.

I did some research and found out that using playerprefs happens to be the best method for saving locally, therefore I wrote with the following script.

 #pragma strict
 
 var textTimer         :     GUIText;
 var timer             :   int = 0; // start timer 
 var minutes         :     float = 0;
 var seconds         :     float = 0;
 var stoptime        :    float = 0;
 var normaleSize     :    float = 20;
 var newSize            :    float = 30;
 var changefont         :    float = 0;
 var time             :    float = 0;
 var increaseSize    :    boolean = false;
 var timeActive      :    boolean = true;
 var asteroid        :    GameObject;
 var scoreText       :   GUIText;
 
 function Update () {
 
     if (increaseSize == true) {
     
     time += Time.deltaTime;

     // this basically gets the normal size of the text and changes it to the new size
     // which is achieved by getting the value of time and dividing it by the font size

     textTimer.fontSize = Mathf.Lerp ( normaleSize, newSize, time / changefont);
     
     }
 
      textTimer.text = "Timer: " + minutes + ":" + seconds;
      
      if (timeActive) {
      timer             = Time.timeSinceLevelLoad;
      minutes         = (timer / 60); //
     seconds         = (timer % 60);
     
       }
 }
 
 function StopTimer () {
 
         timer         = 0;
          timeActive     = false;
          print ("time stoped!");
          yield WaitForSeconds (3);
          animation.Play();
 }
 
 function FontSize () {
         
         increaseSize = true;
         //textTimer.fontSize = 30;
         //animation.Stop ();
 }
 
 function HighscoreUpdate () { // Here's the problem
     
     if ( timer > PlayerPrefs.GetInt("TimeScore")) { // If timer value happens to be greater then the saved one, then... ---- This isn't working ----
     
         PlayerPrefs.SetInt ("TimeScore", timer); // captures the current time and saves it
     
             NewScore (); // This functions displays the capture time in a GUIText
     }
     
     //NewScore (); // By enabling this, the script works***
 }
 
 function NewScore () {
 
     scoreText.text = "Highscore: " + minutes + ":" + seconds + PlayerPrefs.GetInt ("TimeScore"); // the capture time is display on a GUIText
 
 }

Unfortunately my timer isn't being saved nor displayed, I don't have a single idea why this is happening. I basically wanted that if timer happens to be greater then the previous saved time... then save the new time and display it as a new highscore.

However if I were to enable the //NewScore ();, then the time is displayed in the highscore, but restarting the level causes it to reset every time.

Does anyone have an idea on how to fix this problem? Any help is highly appreciated!

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 getyour411 · Jan 26, 2014 at 09:00 PM 0
Share

Edit - nvm, I thought perhaps you were using SetInt with a float, but I see you have it cast as int.

1 Reply

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

Answer by Sundar · Jan 26, 2014 at 06:44 PM

Here you go

 function HighscoreUpdate () {
  
     if(PlayerPrefs.HasKey("TimeScore") {
         Debug.Log( "Current timer " + PlayerPrefs.GetInt("TimeScore") );            
         if( timer > PlayerPrefs.GetInt("TimeScore")){ 
            Debug.Log("Saved new timer value " + timer );
            PlayerPrefs.SetInt ("TimeScore", timer); 
         }
     }
     else{  
          Debug.Log( "Created new key at PlayerPref for timer " +timer );
          PlayerPrefs.SetInt ("TimeScore", timer);
     }
      NewScore ();
      
 }
 
Comment
Add comment · Show 2 · 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 Ful · Jan 26, 2014 at 08:07 PM 0
Share

Hi, thank you for your response this did make the highscore appear, but it doesn't save the it whenever I reload the level. Any other suggestions?

avatar image Sundar · Jan 26, 2014 at 08:57 PM 0
Share

Added some debugs which will give you some light.

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

19 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

Related Questions

Timer highscore trouble 1 Answer

Is it possible to set a format for a playerprefs value? (Timer) 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

pause timer or player using Time.timeScale 1 Answer

Storing in PlayerPref based on timer 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