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 MarceloTerreiro · Aug 15, 2013 at 01:33 AM · javascriptplayerprefsscorehighscores

PlayerPrefs not saving my variable value for HighScore System

Ok, so I have this code in my restart scene for setting up a highScore variable, if its bigger than the previous one.

 private var scoreSaver : float;// Time of survive
 private var playerHighScore : float; // Time of survive for highscore uses
 private var storeHighScore : float = 0;
 
 function Start ()
 {
     scoreSaver =  PlayerPrefs.GetFloat("Time Survived"); //Retrieves my survive time of previous code(I've set up it correctly in other code)
     playerHighScore = PlayerPrefs.GetFloat("High Score"); Retrieves my survive time of previous code. Equals above, but I need two of this timer, because one of them resets every restart button click.
     if (scoreSaver >= playerHighScore && scoreSaver >= storeHighScore)
     {
         
         storeHighScore = playerHighScore;
         
     }

}

In the restart button, I have this:

 PlayerPrefs.DeleteKey("Time Survived");

The problem is that every time I play, the storeHighScore doesn't get its value, it gets reseted, so I'm always having a new highScore, event if its lower than the previous one

Comment
Add comment · Show 2
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 AlucardJay · Aug 15, 2013 at 07:17 AM 0
Share

When/where are you assigning storeHighScore back to PlayerPrefs?

 PlayerPrefs.SetFloat( "High Score", storeHighScore );

http://docs.unity3d.com/Documentation/ScriptReference/PlayerPrefs.SetFloat.html

avatar image MarceloTerreiro · Aug 15, 2013 at 04:13 PM 0
Share

In a OnGUI Function of other scene and other script.

 PlayerPrefs.SetFloat("Time Survived", difficultyTimer); //SAVES PLAYER SURVIVE TI$$anonymous$$E
     PlayerPrefs.SetFloat("High Score", difficultyTimer);

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by MarceloTerreiro · Aug 19, 2013 at 08:58 PM

Finally I fixed it. It's pretty logic and simple now.

private var timeSurvived : float;

 function Awake()
 { 
     var highScore = PlayerPrefs.GetFloat("highScore"); 
     timeSurvived = PlayerPrefs.GetFloat("timeSurvived");    
     
     if (timeSurvived > highScore)
     {
         PlayerPrefs.SetFloat("highScore", timeSurvived);
     }    
 }

My problem was assigning the highScore variable, but I've discovered I needed to make it equals to PlayerPref highScore float. After that, I did not have any other problems. This also doesn't creates multiple playerPreference.

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 tw1st3d · Aug 19, 2013 at 08:59 PM 0
Share
  • for posting your resolution.

avatar image
0

Answer by ThomasB · Aug 19, 2013 at 07:28 PM

 if(PlayerPrefs.HasKey("HScore"))
 {
     if(PlayerPrefs.GetInt("HScore")<curScore)
         { 
             PlayerPrefs.SetInt("HScore", curScore);
             
         }
 }else{
     PlayerPrefs.SetInt("HScore",curScore);
 }

Follow a similar structure to prevent new highscores being set all the time.

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

Answer by meat5000 · Aug 19, 2013 at 07:43 PM

Make a separate gameObject called HighScoreKeeper or something with a script which reads the

  public var playerHighScore

from your above script.

if you have something like on whatever is keeping score:

     function Update()
     {
             currentScore = //whatever;
     
             if ( currentScore >= highScoreKeeper.GetComponent(highScoreScript).highScore )
             (
                 playerHighScore = currentScore;
             }
 
     }

then in HighScoreKeeper object have something like:

public var highScore : int = 0; var pHighScore : int = 0;

 function Update()
 {
      pHighScore = scoringObject.GetComponent(scoringScript).playerHighScore;
 
      if ( pHighScore >= highScore )
      {
          highScore = pHighScore;
      }
                    
         
 }
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.

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

Is there a decent tutorial for a local high score table (android)? 1 Answer

Highscoring and changing scripts (Java/UnityScript) 1 Answer

PlayerPrefs HighScore problems, it doesn't work. 4 Answers

high scores scene 2 Answers

in this code whenever score is gretater than highscore then old highscore becomes zero and after the game ends it get updated 0 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