- Home /
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?
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.
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.
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.
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.
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.
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!!
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();
.
I have now also tried it out on my android phone, and it works great.