Advertisement

My high score do not update after restart

Started by November 07, 2014 05:09 AM
3 comments, last by MNQ91 10 years, 2 months ago

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

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;
}
 
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<GameOverScript> ();
HealthScript.instance.gameEnd ();
 
}

I don't know how gameEnd is called or how your gameoverscript works but I'll make a guess that


points = highscore;

is propably wrong way around and should be


highscore = points;

.

Advertisement

I don't know how gameEnd is called or how your gameoverscript works but I'll make a guess that


points = highscore;

is propably wrong way around and should be


highscore = points;

.

Oh my God, thanks PunCrathod.
but i need explanation, does it matter with that? i think it just same..

= is an assign operator. What it basically does is put whatever is on the right side into whatever is on the left side. Tough I can see how people new to programming could assume it would work differently.

= is an assign operator. What it basically does is put whatever is on the right side into whatever is on the left side. Tough I can see how people new to programming could assume it would work differently.

well thanks for the explanation! biggrin.png biggrin.png biggrin.png biggrin.png

This topic is closed to new replies.

Advertisement