showing Highscore GUI

Hey guys, how do I go abouts implementing a simple highscore system. this is my scoring based code

using UnityEngine;
using System.Collections;

public class Score : MonoBehaviour 

{
	public GUIText JewelsText;
	public GUIText WinText;
	private int Jewels;


	void Start () 
	{
		Jewels = 0;
		SetJewelsText();
		WinText.text = "";
	}
	
	void OnTriggerEnter(Collider other)
	{
		
		if(other.gameObject.tag == "Pickups")
		{
			other.gameObject.SetActive(false);
			Jewels = Jewels + 1;
			SetJewelsText();
		}
	}
	
	void SetJewelsText()
	{
		JewelsText.text = "Jewels:" + Jewels.ToString();
		if(Jewels >=2)
		{
			WinText.text = "You Collected all the jewels!";
		}
	}
}

I want it to so save the previous score but reset if its beaten next time round

"[...] setting it or create your own method since you have them all under the same parent object you can use GetComponentsInChildren". Sometimes, OP asks for something that could have been done easier in a different manner. So it is common to give an answer that does not answer the question but more likely provide a different (sometimes more suitable) solution to the problem. In this case, 3 years ago (time goes by so fast), I was saying that there is this or there is an answer.

1 Answer

1

Create another variable, store high score there. Then just compare these two variables on every OnTriggerEnter.

var int highScore = 0;

void OnTriggerEnter (Collider other)
if (Jewels > highScore) {
highScore = Jewels
}

Don't forget that if you close your game or reload scene, highScore value will be restore to zero. You need to use some sort of saving system, like PlayerPrefs (if you are a beginner, you should watch some tutorial). [PlayerPref Documentation][1] [First tutorial i found :), it looks good][2] [1]: http://docs.unity3d.com/Documentation/ScriptReference/PlayerPrefs.html [2]: http://forum.unity3d.com/threads/119440-Unity3d-Tutorial-PlayerPrefs-(Saving)

Thanks mate, ill have ago at it now :)