- Home /
My high score do not update after restart
hello, i got a problem here. I'm developing shooting game. Then, i need to display the high score but it's doesn't shown. It's never update the score after restart. What should i do? I can't figure out the problem right now.
here my health script (attached to every enemy)
using UnityEngine;
public class HealthScript : MonoBehaviour
{
public static HealthScript instance;
public int hp = 1;
private GUIText scoreReference;
private GUIText highscoreReference;
private static int _highscore = -1;
public int highscore {
get { if (_highscore == -1)
_highscore = PlayerPrefs.GetInt("Highscore", 0);
return _highscore;
}
set {
if (value > _highscore) {
_highscore = value;
highscoreReference.text = _highscore.ToString();
PlayerPrefs.SetInt("Highscore", _highscore);
}
}
}
public bool isEnemy = true;
private static int points;
public void Damage(int damageCount) {
hp -= damageCount;
if (hp <= 0)
{
// Dead!
Destroy(gameObject);
points++;
scoreReference.text = points.ToString();
}
}
public void gameEnd() {
points = highscore;
points = 0;
}
//update from previous code
void Start()
{
scoreReference = GameObject.Find("Score").guiText;
highscoreReference = GameObject.Find("HighScore").guiText;
scoreReference.text = points.ToString();
highscoreReference.text = highscore.ToString ();
instance = this;
}
then this is my player script where i put the game over method
void OnDestroy()
{
// Game Over.
// Add the script to the parent because the current game
// object is likely going to be destroyed immediately.
transform.parent.gameObject.AddComponent ();
HealthScript.instance.gameEnd ();
}
UPDATE: THE PROBLEM SOLVED. JUST CHANGE THIS "points = highscore;" TO "highscore = points;". I DON'T KNOW BUT DOES IT MATTER TO BE LIKE THAT? I HOPE SOMEONE CAN EXPLAIN THAT..THANKS EVERYONE!
Answer by ashique · Nov 07, 2014 at 09:17 AM
You need to check the condition using PlayerPrefs only, u r using class variables to check the condtion those will be reset on restart...
if (_highscore == -1) _highscore = PlayerPrefs.GetInt("Highscore"); // GetInt dont take 2 //parameter
get {
return PlayerPrefs.GetInt("Highscore"); } set { if (value > PlayerPrefs.GetInt("Highscore")) { highscoreReference.text = _value.ToString(); PlayerPrefs.SetInt("Highscore", value); } }
it still the same, the highscore do not update after restart...
get { if (_highscore == -1)
_highscore = PlayerPrefs.GetInt("Highscore");
return _highscore;
get { highscoreReference.text = PlayerPrefs.GetInt("Highscore"); return PlayerPrefs.GetInt("Highscore"); } set { if (value > PlayerPrefs.GetInt("Highscore")) { highscoreReference.text = _value.ToString(); PlayerPrefs.SetInt("Highscore", value); } }
Answer by ExtremePowers · Nov 07, 2014 at 11:33 AM
I think this would do it:
public int highscore {
get { if (_highscore == -1)
_highscore = PlayerPrefs.GetInt("Highscore");
return _highscore;
}
set {
if (value > _highscore) {
_highscore = value;
highscoreReference.text = _highscore.ToString();
PlayerPrefs.SetInt("Highscore", _highscore);
PlayerPrefs.Save();
}
}
}
hey thanks, it doesn't work, what wrong with this code?
using UnityEngine;
public class HealthScript : $$anonymous$$onoBehaviour
{
public static HealthScript instance;
public int hp = 1;
private GUIText scoreReference;
private GUIText highscoreReference;
private static int _highscore = -1;
public int highscore {
get { if (_highscore == -1)
_highscore = PlayerPrefs.GetInt("Highscore");
return _highscore;
}
set {
if (value > _highscore) {
_highscore = value;
highscoreReference.text = _highscore.ToString();
PlayerPrefs.SetInt("Highscore", _highscore);
PlayerPrefs.Save();
}
}
}
public bool isEnemy = true;
private static int points;
public void Damage(int damageCount) {
hp -= damageCount;
if (hp <= 0)
{
// Dead!
Destroy(gameObject);
points++;
scoreReference.text = points.ToString();
}
}
public void gameEnd() {
points = highscore;
points = 0;
}
void Start()
{
scoreReference = GameObject.Find("Score").guiText;
highscoreReference = GameObject.Find("HighScore").guiText;
scoreReference.text = points.ToString();
highscoreReference.text = highscore.ToString ();
instance = this;
}
Your answer
Follow this Question
Related Questions
cant delay with WaitForSeconds C# 2 Answers
Distribute terrain in zones 3 Answers
"using Android" doesn't recognizable on Unity 1 Answer
Android Development; How to use SDK manager! 1 Answer
Anisprite animation from OnTriggerEnter 0 Answers