Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 miapricana · Jan 15 at 07:44 PM · playerprefsscene-loadingsingletonawake

How to create a main menu with singleton pattern?

Hello everyone, I am new both to this forum and Unity, so please bare with me, my understanding of this might not be the best. I am using a game manager to manage my game data (lives, score, position in player prefs) and game scenes. My GameManagerObject is in the game scene and I created a singleton pattern in awake method of game manager script. In order to switch to game over or win scene after the player dies or wins, I am using OnSceneLoaded method, and also enabling it in Awake. Now my question is, how do I implement a main menu, or create a retry button on game over scene which will lead to my game scene again, as the singleton won't let me execute the start method the second time? Basically, I believe because of singleton, when my game is run directly from the scene everything runs accordingly, but when I load the game through another scene I am getting an error saying that the GameObjectManager has been destroyed.

I also had an issue where the playerPrefs "weren't loading" when starting the game from another scene (e.g. player lives were null), and now I managed to fix it but they are still not saving and loading.

This a part of my code. Disclaimer, I am supposed to use a singleton so I'm mainly searching for solutions including its use.. Thanks

     private void Awake()
     {
         //singleton 
         if (_instance == null)
         {
             _instance = this;
         }
         else if (_instance != this)
         {
             Destroy(this.gameObject);
         }
 
         SceneManager.sceneLoaded += OnSceneLoaded; //for gameover
 
         DontDestroyOnLoad(this.gameObject);
     }
 
     void Start()
     {
         //there's more but basically loading and displaying data
         GetComponent<SaveLoadManager>().LoadData();
     }

And these are my GameOver and Win scenes.

     void OnSceneLoaded(Scene scene, LoadSceneMode mode)
     {
 
         if (scene.name == "GameOver" || scene.name == "Win")
         {
             Text scoretext = GameObject.Find("Scoretext").GetComponent<Text>();
             scoretext.text = "score: " + GameData.GameScore.ToString();
 
             if (GameData.GameScore > GameData.HighScore) GameData.HighScore = GameData.GameScore;
             Text highscoretext = GameObject.Find("Highscore").GetComponent<Text>();
             highscoretext.text = "highest score: " + GameData.HighScore.ToString();
             //also displaying and saving newly set data
         }
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Beacom · Jan 15 at 08:04 PM

Is there a reason not to call LoadData() in either the Awake() function or OnSceneLoaded() function? Your singleton setup looks good. As you stated Start() is only executed once, so I think you just need to move any logic you have in Start() that needs multiple executions to a function that will be called when appropriate.

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 miapricana · Jan 16 at 12:29 AM 0
Share

I tried placing the necessary code in another function instead of inside Start() and calling it when the Game scene is loaded, I even used code to check if the scene is loaded first to prevent returning null values, but it didn't seem to work. When being called from Awake() it does work, but doesn't load saved data, just sets and displays its values to what I stated in the script itself.

avatar image
0

Answer by rh_galaxy · Jan 16 at 04:07 AM

I think in Awake() you should return after destroying the object, and also be aware that destroying the object will make OnDestroy() get called, so don't do any deinit there, have it empty.

     else if (_instance != this)
     {
         Destroy(this.gameObject); //be careful, calls OnDestroy()
         return;
     }
     //the rest is done once only...
     //...

I'm unsure if Start() is called the second time after Awake, try having it like the other answer says when you want data to be loaded.

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

140 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Why my code seems skipped after singleton pattern ? 1 Answer

singleton becomes null? 0 Answers

Move a gameobject after loading new scene? 2 Answers

How to call Screen.SetResolution() only once before the first scene starts loading ? 1 Answer

Help with loading player position after scene change 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