Hey! I have finally gotten my first game ever (basically spaceinvaders) to function, just how I want! (yay!) but… I need to add one last thing - I have a “Highscore” that functions, but… it doesn’t save.
Could anyone help me with this? I googled and found some things on UnityAnswers already, but I get an error when trying to call the function that I found that should do it ( to be completely honest, I barely understand the code that’s meant to save it in the first place)
I’ll link my code below -
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class highScoreController : MonoBehaviour {
public Text highScore;
public GameObject scoreTimer;
score myScore;
public int myHighScore;
// Use this for initialization
void Start ()
{
GameObject scoreTimer = GameObject.Find("scoreTimer");
myScore = scoreTimer.GetComponent<score>();
highScore.GetComponent<Text>().text = "HighScore: " + myHighScore.ToString();
//replace next line with save data
myHighScore = 0;
}
// Update is called once per frame
void Update ()
{
if (myScore.scoreme > myHighScore)
{
myHighScore = myScore.scoreme;
highScore.GetComponent<Text>().text = "HighScore: " + myHighScore.ToString();
//trying to call function below - getting error....
StoreHighscore();
}
}
// function below is completely copy & paste
void StoreHighscore(int newHighscore)
{
int oldHighscore = PlayerPrefs.GetInt("highscore", 0);
if (newHighscore > oldHighscore)
PlayerPrefs.SetInt("highscore", newHighscore);
PlayerPrefs.Save();
}
}