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 joshualhowland · Aug 10, 2014 at 03:27 AM · c#playerprefsscore

What am I doing wrong with PlayerPrefs?

I have been trying for days now to get my game to locally save the High Score. I have read just about every post, and watched numerous videos on the subject. I still cannot get this to work. Here is the C# code I am using:

 using UnityEngine;
 using System.Collections;
 
 public class ScoreManager : MonoBehaviour {
 
     public static int highScore = PlayerPrefs.GetInt ("HighScore", 0);
 
     void AddScore() {
 
 
         int score = Destroyer.score;
 
                 if (score > highScore)
                         highScore = score;
                         PlayerPrefs.SetInt ("HighScore", score);
         }
 }

I have it collecting the current score from this script, which works just fine keeping the current score:

 using UnityEngine;
 using System.Collections;
 public class Destroyer : MonoBehaviour {
 
     public static int score = 0;
 
     void OnTriggerEnter2D (Collider2D other){
         score = score + 1;
         Destroy (other.gameObject);
     }
     
         void Start(){
             
     }
 }

Like I said, I don't know why this doesn't work. From everything I have read, it should. What am I doing wrong, and where is the best place to run this code?

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 rutter · Aug 10, 2014 at 04:19 AM 0
Share

Is anything calling Score$$anonymous$$anager.AddScore? You might try Debug.Log when it's called, just to make sure your values are what you'd expect.

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by NoseKills · Aug 10, 2014 at 09:27 AM

Hard to say if this is the only problem since "doesn't work" is not a lot of info but at leadt you are writing the score to prefs every time regardless of whether it's bigger or smaller than the old score.

 if (score > highScore)
    highScore = score;
    PlayerPrefs.SetInt ("HighScore", score);


Because you are not using curly brackets, the effective range of the if-block is until the first semicolon after it.

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 joshualhowland · Aug 10, 2014 at 05:34 PM 0
Share

I ended up just cutting out the highscore = score; line and moving it to the same script that keeps track of current score. Thank you, this helped a lot.

avatar image
0

Answer by joshualhowland · Aug 10, 2014 at 05:38 PM

Okay...I got it to work. I ended up just moving the code to the same code that keeps the current score. Here it is:

 using UnityEngine;
 using System.Collections;
 public class Destroyer : MonoBehaviour {
 
     public static int score = 0;
     public static int highScore = PlayerPrefs.GetInt ("HighScore", 0);
 
     void OnTriggerEnter2D (Collider2D other){
         score = score + 1;
         if (score > highScore) {
             PlayerPrefs.SetInt ("HighScore", score);
             Debug.Log ("HighScoreSet");
         }
         Destroy (other.gameObject);
     }
     
         void Start(){
 
     }
 }

Then to display the high score in game I used this code:

 void Update () {
         guiText.text = "HIGH SCORE : " + PlayerPrefs.GetInt ("HighScore");
     }

Eventually I am going to have a high score for each difficulty (easy, medium, hard). When I figure out how to do that, I will post that code as well.

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 joshualhowland · Aug 10, 2014 at 07:19 PM 0
Share

Okay...finished...Here is the code I am using to separate the high scores:

 using UnityEngine;
 using System.Collections;
 public class Destroyer : $$anonymous$$onoBehaviour {
 
     public static int score = 0;
     public static bool easy;
     public static bool medium;
     public static bool hard;
 
     void OnTriggerEnter2D (Collider2D other){
         score = score + 1;
 
         if (easy == true & score > PlayerPrefs.GetInt ("HighScoreEasy"))
                 PlayerPrefs.SetInt ("HighScoreEasy", score);
 
         if (medium == true & score > PlayerPrefs.GetInt ("HighScore$$anonymous$$edium"))
             PlayerPrefs.SetInt ("HighScore$$anonymous$$edium", score);
 
         if (hard == true & score > PlayerPrefs.GetInt ("HighScoreHard"))
             PlayerPrefs.SetInt ("HighScoreHard", score);
             
             
 
         Destroy (other.gameObject);
     }
     
         void Start(){
 
     }
 }

Then an example of my button to choose what difficulty you play:

 using UnityEngine;
 using System.Collections;
 
 public class EasyButton : $$anonymous$$onoBehaviour {
     
     // Use this for initialization
     void Start () {
         
     }
     
     // Update is called once per frame
     void Update () {
         
     }
     void On$$anonymous$$ouseDown(){
         Destroyer.score = 0;
         Destroyer.easy = true;
         Destroyer.medium = false;
         Destroyer.hard = false;
         Game$$anonymous$$aster.difficulty = 1.5f;
         Application.LoadLevel ("BABYSPLAT");
     }
 }

Now, my script to display the high score at game over:

 using UnityEngine;
 using System.Collections;
 
 public class HighScore : $$anonymous$$onoBehaviour {
 
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
         if (Destroyer.easy == true)
         guiText.text = "HIGH SCORE : " + PlayerPrefs.GetInt ("HighScoreEasy");
 
         if (Destroyer.medium == true)
             guiText.text = "HIGH SCORE : " + PlayerPrefs.GetInt ("HighScore$$anonymous$$edium");
 
         if (Destroyer.hard == true)
             guiText.text = "HIGH SCORE : " + PlayerPrefs.GetInt ("HighScoreHard");
 
 
     }
 }

I hope this helps anyone else wondering how to do this. I don't know if this is the best, prettiest, or easiest way to accomplish storing multiple high scores, but it works.

avatar image
0

Answer by FrosTech · Aug 10, 2014 at 06:10 PM

Have you ever considered saving the data to playerprefs at all.

I mean your codes are great, but in no part of your code, the PlayerPrefs.Save() method is called to save datas.


// Saves data to playerprefs permanently
PlayerPrefs.Save();

Note: You wont need this if you only want data for temporary usage, otherwise I can't say!!

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 joshualhowland · Aug 10, 2014 at 07:44 PM 0
Share

I have built the game as a windows development build and it saves all of my scores every time. I have played it and gotten a high score, closed it out, reopened it, and my score is still there. So I am not sure I need to use the PlayerPrefs.Save();.

avatar image joshualhowland · Aug 11, 2014 at 05:57 AM 0
Share

I have now also tried it out on my android phone, and it works great.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Saving final score and displaying on main menu 1 Answer

Is there a way to add one to playerprefs? 2 Answers

Where am I going wrong with these scripts? 1 Answer

increase playerprefs 1 Answer

Problems with saving/loading score with PlayerPrefs [C#] 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