Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by TheSaviour · May 19, 2016 at 05:12 PM · c#playerprefsdontdestroyonloadhighscoremainmenu

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
0

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image TheSaviour · May 20, 2016 at 10:20 AM 0
Share

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.

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

PlayerPrefs to save setActive state 2 Answers

How to stop objects you picked up from reappearing when you go to another level and return 1 Answer

PlayerPrefs highscore system? 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges