I’ve been watching tutorials and reading similar topics but to be honest, I’m not finding them very helpful. So far, I’ve successfully managed to use Dontdestroyonload and PlayerPrefs to display the high score onto the main menu scene. However, I haven’t found a way to save the score onto the main menu scene. When I exit play mode and re-enter play mode, the high score is no longer displayed on the main menu.
Here’s what I did so far:
i) I’ve added a copy of my high score script (called ScoreManager) to an empty game object (called Score) in the game scene.
ii) The main menu will load when I lose the game so I’ve added a script (called RetainHighScore) to the same empty game object (Score) so that it doesn’t get destroyed and stays in the main menu scene
public class RetainHighScore : MonoBehaviour
{
void Start ()
{
DontDestroyOnLoad (gameObject);
}
}
iii) I created a canvas in the main menu scene and added a text to it. Finally, I added this script to the canvas:
public class MainMenu : MonoBehaviour
{
public ScoreManager scoreManager; //reference to the ScoreManager script attached to Score
public Text highScoreText; //reference to the text that will display the high score
public void Start()
{
scoreManager = GameObject.Find ("Score").GetComponent<ScoreManager> ();
}
public void Update()
{
highScoreText.text = "High Score:" + Mathf.Round(scoreManager.highScore);
}
}
So as I said, this displays the high score in the main menu scene as expected, but only after the main menu scene loads when I lose the game, while still in play mode. When I re-enter play mode from the main menu scene, the high score is no longer there. Obviously, this is because the Score game object containing the ScoreManager script and the RetainHighScore script no longer exists when I enter play mode from the menu scene.
So I wanna know how I can save the high score from the ScoreManager script and keep it displayed in the main menu at all times.