I have a score script right here:
using UnityEngine;
using System.Collections;
public class ScoreSystem : MonoBehaviour {
public GUIText guiText;
public int scoreValue;
public static bool hasPlayed = false;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(Move.playerAlive) {
scoreValue++;
}
guiText.text = "" + scoreValue;
PlayerPrefs.SetInt("scoreValue", scoreValue);
if(!Move.playerAlive) {
PlayerPrefs.Save();
hasPlayed = true;
}
}
}
What I want is to have another PlayerPrefs value, that will only record the highest score (high score). Can anyone help me out?
Thanks in advance!
pretty simple, just say:
if(PlayerPrefs.GetInt("High Score") < scoreValue)
PlayerPrefs.SetInt("High Score", scoreValue);
You will need an if statement that checks the current score against the stored highscore:
// at the top
public int highScore;
// in your start functiion
void Start(){
highScore = PlayerPrefs.GetInt("highScore");
if (highScore == 0){
highScore = 0;
}
}
// in your update function
void Update(){
// ...
if (scoreValue > highScore){
PlayerPrefs.SetInt("highScore", scoreValue);
} else {
PlayerPrefs.SetInt("highScore", highScore);
}
}
I think this as a basic recipe may help you get started.
// Declare variables
private int highScore;
public int scoreValue;
// Use this for initialization
void Start(){
if (HasKey("highScore"))
{
highScore = PlayerPrefs.GetInt("highScore");
}else
{
PlayerPrefs.SetInt("highScore", 0);
highScore = 0;
}
}
// Update is called once per frame
void Update(){
if (scoreValue > highScore){
PlayerPrefs.SetInt("highScore", scoreValue);
highScore = scoreValue ;
}
}