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 Zypher · Jun 12, 2014 at 06:53 PM · playerprefsscore system

How do I use PlayerPrefs to save My Score?

As the question states, I want to display and save the player's best score on the Game Over Scene. And every time the player beats their score, it replaces the previous one.

Here is my Score Script:

 #pragma strict
 static var currentScore : int = 0;
 
 var offsetY : float = 40;
 var sizeX : float = 100;
 var sizeY : float = 40;
 function Awake()
 {
       currentScore = 0;
 }
 function OnGUI () {
     GUI.color = Color.yellow;
     GUI.Box (new Rect (Screen.width/2-sizeX/2, offsetY, sizeX, sizeY), "" + currentScore);
 }

This is in my player script:

        GameMaster.currentScore += 1;
 
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

2 Replies

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

Answer by Daniel-Everland · Jun 12, 2014 at 09:47 PM

PlayerPrefs work by getting and setting values to a file.

To set an int value, use the following function. Name of file being whatever you'd like to call it. This will also overwrite any existing playerpref with the same name

 PlayerPrefs.SetInt("NameOfFile", currentScore);

To read a playerpref, say at game startup, use the following function

 currentScore = PlayerPrefs.GetInt("NameOfFile");
Comment
Add comment · Show 8 · 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 Zypher · Jun 12, 2014 at 10:46 PM 0
Share

Will it matter if I place it within this:

 function GameOver(){
     Application.LoadLevel("Game Over");
 }
avatar image Daniel-Everland · Jun 13, 2014 at 10:56 PM 0
Share

No, you can definitely do that. Just make sure Application.LoadLevel("Game Over"); is the last thing in the function, otherwise I'm pretty sure it won't get called :)

avatar image Zypher · Jun 14, 2014 at 02:16 AM 0
Share

I'm not sure if this is correct, but:

 function GameOver(){
     //save highscore
 if(score > PlayerPrefs.SetInt("Game$$anonymous$$aster", currentScore);
 {
     currentScore = PlayerPrefs.GetInt("Game$$anonymous$$aster");
 }
     Application.LoadLevel("Game Over");
 }

I am getting a error in which it states: Assets/GameOver.js(9,58): BCE0044: expecting ), found ';'.

avatar image Daniel-Everland · Jun 14, 2014 at 02:22 AM 0
Share

Not quite. In your if statement what you're basically doing is saying "If score is larger than set "Game$$anonymous$$aster" to currentscore" - as you can tell, that doesn't make sense. Also, within the if statement you're setting currentscore to whatever "Game$$anonymous$$aster" is, which is syntaxically correct, but used in the wrong context. Do the following ins$$anonymous$$d.

$$anonymous$$eep in $$anonymous$$d that after you've typed in parameters there's no need to use a semicolon, which is what the error is referring to :)

 function GameOver(){
     //save highscore
     if(score > PlayerPrefs.GetInt("Game$$anonymous$$aster"))
     {
         PlayerPrefs.SetInt("Game$$anonymous$$aster", currentScore);
     }
     Application.LoadLevel("Game Over");
 }

In the above example we're doing the following

If score is larger than "Game$$anonymous$$aster" Set "Game$$anonymous$$aster" to currentScore.

Hope this clarifies it for you :)

avatar image Zypher · Jun 14, 2014 at 02:37 AM 0
Share

For some reason I'm still receiving the same error message. $$anonymous$$aybe I need to make a reference to my score script.

Thank You for your help. :)

Show more comments
avatar image
2

Answer by Ziaxp · Jun 08, 2015 at 12:57 PM

You should check the Key first, if it exist then it will get score value from PlayerPrefs, otherwise will SetInt the PlayerPrefs. Do as following.

          if (PlayerPrefs.HasKey("highScore") )
         {
             if (GameScore.newScore > PlayerPrefs.GetInt("highScore") ) 
             {
                 highScore = GameScore.newScore;
                 PlayerPrefs.SetInt("highScore", highScore);
                 PlayerPrefs.Save();
             }
         }
         else
         {
             if (GameScore.newScore > highScore ) 
             {
                 highScore = GameScore.newScore;
                 PlayerPrefs.SetInt("highScore", highScore);
                 PlayerPrefs.Save();
             }    
         }

         Application.LoadLevel("GameOver");
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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Detailed Help on using PlayerPrefs? 1 Answer

Im Doing HighScore System To My Game And There Is Something Wrong In My Scripts 0 Answers

Change store balanced based on level scores. 1 Answer

One PlayerPref not saving 1 Answer

Argument out of range 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