• 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
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

People who like this

0 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 TIME
     PlayerPrefs.SetFloat("High Score", difficultyTimer);

3 Replies

  • Sort: 
avatar image

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
tw1st3d

People who like this

1 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

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

People who like this

0 Show 0 · 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

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

People who like this

0 Show 0 · 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

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

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

Highscoring and changing scripts (Java/UnityScript) 1 Answer

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

Is it necessary to create an Array() to show high scores in PlayerPref? 1 Answer

High Score with player prefs confusion (JAVA) 0 Answers

Recognising high score when updated with a graphic 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