Forum Thread
  Posts  
My high score do not update after restart (Forums : Coding & Scripting : My high score do not update after restart) Locked
Thread Options
Nov 7 2014 Anchor

hello, i got a problem here. I'm developing shooting game in unity. Then, i need to display the high score but it's doesn't shown. What should i do? I can't figure out the problem right now.This is android game and c# language

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 ();
    HealthScript.instance.gameEnd ();
}
Reply to thread
click to sign in and post

Only registered members can share their thoughts. So come on! Join the community today (totally free - or sign in with your social account on the right) and join in the conversation.