Hi, i am looking for an answer on how to add a ‘New High Score’ to the below script. I only want this to be stored localy and only show if the old high score has been beaten.
Basically, i want it to read
Game Over
Score
New High Score (if it is a new high score)
I know that there are other answers to this, and people are going to tell me to read those, but i have read those and cant work out how to transform them into my script.
This is all i need to do to complete my little game, and then i will be going back to basics and learning it all from scratch.
I just really want to get this game finished so my son can play it while i am learning how to improve.
Any help with this would be very appreciated.
Thanks
using UnityEngine;
using System.Collections;
public class GameOverScript : MonoBehaviour {
public GUISkin mySkin;
int score = 0;
void Start () {
score = PlayerPrefs.GetInt ("Score");
}
void OnGUI()
{
GUI.skin = mySkin;
GUI.Label (new Rect (Screen.width / 2 - 100, Screen.height / 2 - 10, 500, 100), "SCORE: " + score, "label");
if (GUI.Button (new Rect (Screen.width / 2 - 100, Screen.height / 2 + 150, 60, 30), "Retry", "label"))
Application.LoadLevel (1);
}
}
int score = 0;
int score_thisRound = 0;
void Start(){
if(PlayerPrefs.HasKey("Score")){
score = PlayerPrefs.GetInt ("Score");
}
}
void OnGUI(){
GUI.skin = mySkin;
if(score_thisRound > score){
GUI.Label (new Rect (Screen.width / 2 - 100, Screen.height / 2 - 10, 500, 100), "SCORE: " + score, "label");
}
if (GUI.Button (new Rect (Screen.width / 2 - 100, Screen.height / 2 + 150, 60, 30), "Retry", "label")){
if(score_thisRound > score){
PlayerPrefs.SetInt ("Score",score_thisRound);
}
Application.LoadLevel (1);
}
}
Ok, I think I get what you’re trying to do, although it is a little confusing. I think that the score variable is the score for the current game? In which case if you want to store the high score you can just use the PlayerPrefs again.
int score;
int highScore;
bool newHighScore;
void Start () {
score = PlayerPrefs.GetInt ("Score");
if (!PlayerPrefs.HasKey("HighScore")) {
PlayerPrefs.SetInt("HighScore", score);
newHighScore = true;
highScore = score;
}
else {
highScore = PlayerPrefs.GetInt("HighScore");
if (score > highScore) {
newHighScore = true;
PlayerPrefs.SetInt("HighScore", score);
highScore = score;
}
else
newHighScore = false;
}
}
void OnGUI()
{
GUI.skin = mySkin;
if (newHighScore)
GUI.Label (new Rect (Screen.width / 2 - 100, Screen.height / 2 - 10, 500, 100), "Game Over Score New High Score: " + score, "label");
else
GUI.Label (new Rect (Screen.width / 2 - 100, Screen.height / 2 - 10, 500, 100), "SCORE: " + score, "label");
if (GUI.Button (new Rect (Screen.width / 2 - 100, Screen.height / 2 + 150, 60, 30), "Retry", "label"))
Application.LoadLevel (1);
}
you need this
int score =0;
int highScore;
bool newHighScore;
// Use this for initialization
void Start () {
score = PlayerPrefs.GetInt ("Score");
if (!PlayerPrefs.HasKey("HighScore")) {
PlayerPrefs.SetInt("HighScore", score);
newHighScore = true;
highScore = score;
}
else {
highScore = PlayerPrefs.GetInt("HighScore");
if (score > highScore) {
newHighScore = true;
PlayerPrefs.SetInt("HighScore", score);
highScore = score;
}
else
newHighScore = false;
}
}
void OnGUI()
{
GUI.Label (new Rect (Screen.width / 2 - 100, Screen.height / 2 - 50, 400, 80), "High Score: " + highScore, "label");
if (newHighScore)
GUI.Label (new Rect (Screen.width / 2 - 100, Screen.height / 2 - 10, 500, 100), "New High Score: " + score, "label");
else
GUI.Label (new Rect (Screen.width / 2 - 100, Screen.height / 2 - 10, 500, 100), "Score: " + score, "label");
if (GUI.Button (new Rect (Screen.width / 2 - 30, 350, 60, 30), "Retry?"))
{
Application.LoadLevel(0);
}
}