- Home /
How do I save the high score from the game scene onto the main menu scene using PlayerPrefs and Dontdestroyonload?
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.
Answer by tanoshimi · May 19, 2016 at 05:32 PM
Where are you using PlayerPrefs? Because that's what you need to persist scores between games. https://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/persistence-data-saving-loading
PlayerPrefs is being used in the Score$$anonymous$$anager script which is attached to an empty game object that I re-named to "Score". It is this empty game object that will be transferred to the main menu scene.
But anyways, thanks for the link. Looks like it just might have what I'm looking for. I'll check it out and come back if I have further questions.
Answer by Faraonull · Feb 25, 2020 at 01:25 PM
hey, im trying your method of displaying the score in the 2 scenes of my game, however im finding it dificult rezolving soime problems. Did you have any NullReferenceExceptions errors with the line scoreManager = GameObject.Find("score").GetComponent(); ? And how do you reference the text from the main menu.
Answer by Superping44 · May 20, 2020 at 03:53 PM
was this solved? Im struggling so much right now with this and my head is about to explode... @TheSaviour @Faraonull