- Home /
PlayerPrefs HighScore problems, it doesn't work.
Hello community, I want to save highscore when player go to mainscreen or the scene gameOver. But I don't know how to do it and I'm trying to at least save the score but it doesn't Works.
Here's my complete code.
using UnityEngine;
using System.Collections;
public class ScoreScript : MonoBehaviour {
// Interfaz Grafica Script
public GUIText scoreText;
public GUIText highScoreText;
private int score;
//private int newScore;
private int highScore;
public float widthScore = 0.0f;
public float heightScore = 0.0f;
void Start ()
{
score = 0;
UpdateScore ();
}
public void AddScore (int newScoreValue)
{
score += newScoreValue;
UpdateScore ();
}
void UpdateScore ()
{
scoreText.text = "Score: " + score;
highScoreText.text = "Highscore: " + highScore;
scoreText.pixelOffset = (new Vector2 (Screen.width / widthScore, Screen.height / heightScore));
}
public void StoreHighscore(int newHighscore)
{
int oldHighscore = PlayerPrefs.GetInt("highscore", 0);
if(newHighscore > oldHighscore)
PlayerPrefs.SetInt("highscore", newHighscore);
highScore = newHighscore;
UpdateScore ();
}
}
Any help will be apreciate Im turning crazy...
Where call you function StoreHighscore()? $$anonymous$$aybe, call this function when you script loaded new level?
I called here but it doesn't work. I've called on boss dead.
using UnityEngine;
using System.Collections;
/// Comportamiento de la salud de los objetos
public class SaludBossScript : $$anonymous$$onoBehaviour {
/// Numero de puntos de salud
public int ps = 300;
private GameObject Boss;
private ScoreScript scoreScript;
public int ScoreValue;
private int HighScoreValue;
/// Es jugador o enemigo?
public bool esEnemigo = true;
// LLamamos el script score y el tag enemigo1
void Start ()
{
GameObject scoreScriptObject = GameObject.FindGameObjectWithTag ("ScoreScript");
if (scoreScriptObject != null) {
scoreScript = scoreScriptObject.GetComponent <ScoreScript> ();
}
if (scoreScript == null) {
Debug.Log ("No se puede encontrar ScoreScript");
}
Boss = GameObject.FindWithTag ("Boss");
if (Boss == null) {
Debug.Log ("No se encuentra a Boss");
}
}
void OnTriggerEnter2D(Collider2D collider)
{
//Es un disparo?
Disparar disparo = collider.gameObject.GetComponent<Disparar>();
if (disparo != null)
{
// Revisamos si es enemigo o compañero
if (disparo.esDisparoEnemigo != esEnemigo)
{
ps -= disparo.daño;
// Destruimos el disparo
// No colocar solo Destroy()
//sino eli$$anonymous$$ara el Script
Destroy(disparo.gameObject);
if (ps <= 0)
{
// $$anonymous$$uerto + EfectoEspecial de particulas, += score
scoreScript.AddScore(ScoreValue);
EfectosEspecialesScript.Instancia.ExplosionBoss1(transform.position);
EfectosDeSonido.Instancia.ReproducirSonidoBossExplosion();
Destroy(gameObject);
Camera.main.GetComponent<CargarNivelCamaraScript>().myLoading();
scoreScript.StoreHighscore(HighScoreValue);
}
}
}
}
}
When are you call StoreHighScore? also, It seems as you are only using that newHighscore > oldHighscore
comparison to set the playerprefs, and then you ALWAYS set the highScore to the newHighScore parameter of StoreHighscore. Unless you have another check in place before you call StoreHighScore, it is likely that your current highscore is different than the one stored in PlayerPrefs.
Edit: Now that I see the other script: Where is HighScoreValue initialized there? also, as I said, there is no check in place to see if that value is higher than the current highscore.
Answer by zharik86 · Aug 25, 2014 at 07:17 AM
In your second script you initialization not variable "HighScoreValue". It's equal 0 always. But this variable isn't necessary to you. In your first script there is "score" variable. It also should be used. I will rewrite parts of a code which change:
public class ScoreScript : MonoBehaviour {
private int score;
...
public void StoreHighscore() { //Use "score" variable
int oldHighscore = PlayerPrefs.GetInt("highscore", 0);
if(score > oldHighscore)
PlayerPrefs.SetInt("highscore", score);
highScore = score;
UpdateScore ();
}
}
And second script:
public class SaludBossScript : MonoBehaviour {
...
void OnTriggerEnter2D(Collider2D collider) {
...
if (ps <= 0) {
// Muerto + EfectoEspecial de particulas, += score
scoreScript.AddScore(ScoreValue); //you change score value in your script
EfectosEspecialesScript.Instancia.ExplosionBoss1(transform.position);
EfectosDeSonido.Instancia.ReproducirSonidoBossExplosion();
Destroy(gameObject);
Camera.main.GetComponent<CargarNivelCamaraScript>().myLoading();
scoreScript.StoreHighscore(); //checks "score" in your script
}
...
}
}
I hope that it will help you.
Answer by praveee · Aug 24, 2014 at 02:23 PM
Try to getInt without default value.
int oldHighscore = PlayerPrefs.GetInt("highscore");
Answer by mlabarca · Aug 24, 2014 at 03:00 PM
When are you call StoreHighScore? also, It seems as you are only using that newHighscore > oldHighscore
comparison to set the playerprefs, and then you ALWAYS set the highScore to the newHighScore parameter of StoreHighscore. Unless you have another check in place before you call StoreHighScore, it is likely that your current highscore is different than the one stored in PlayerPrefs.
Answer by Ryujose · Aug 25, 2014 at 01:12 PM
Thank you zharik86, it Works, the problema it was that I doesn't call the int score variable to highscore and I wanted to call highscore as a newvaluehighscore and return always 0. Am I wrong?
Have a nice day you all!
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
high scores scene 2 Answers
PlayerPrefs 2 Answers
Saving Highscore 2 Answers
Problem with High Score System using PlayerPrefs [C#] 1 Answer