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 /
  • Help Room /
avatar image
0
Question by CruS · Oct 15, 2015 at 10:56 AM · scenestaticloadlevelinstancedontdestroyonload

Singleton GameManager with DontDestroyOnLoad acting weird

Hi,

I've setup a Script called GameManager which has a static instance variable with DontDestroyOnLoad. It is not part of the hierarchy before game launch and is not instantiated, just created. Therefore, when I exit play mode in testing, the game leaves the GameObject without destroying them.

When the level loads, it checks if there is an instance of the GameManager, if not it will check if there is a GameObject around with the GameManager type, and if not it will create it. I do all this in the getter of the class.

I have a Camera in each scene and I do destroy that on load. The Camera calls the GameManager to "activate" it through the getter on Awake.

The problem that occurs is that for some reason, if I run Level1.scene, everything works fine. If I complete the level the game loads Level2.scene. Everything fine now as well. At this point if I exit the play mode, and re-enter playmode. Level1 is again, fine. But when I complete the level and load Level2.scene, it.. "works" but it does re-create the GameManager. The only difference between the runs is that Level2.scene had no GameManager in the first load. It then created them and for some reason saved them to the scene for the next load.

I guess this would be fine, it would be better if they deleted the GameObject no matter how I left the application. But the question remains, why does it re-create it instead of finding the already exsisting GameManager if that's the case?

To illustrate this another way, the code below generates the following Debug output:

Case 1: (never loaded either scene)

Level1.scene

"no instance" <- expected

"no object" <- expected

"waking up" <- expected

Level2.scene

(nothing) <- expected

Case 2: (quit play and loaded just level 1)

Level1.scene

"waking up" <- expected

"no instance" <- expected

Case 3: (quit play and went through level 1 to level 2)

Level1.scene

"waking up" <- expected

"no instance" <- expected

Level2.scene

"no instance" <- not sure about this one

"no object" <- what?

"waking up"

 public class GameManager : MonoBehaviour {
 
     static GameManager _instance;
 
     static public bool isActive {
         get {
             return _instance != null;
         }
     }
 
     static public GameManager instance
     {
         get
         {
             if (_instance == null)
             {
                 Debug.Log("no instance");
                 _instance = Object.FindObjectOfType(typeof(GameManager)) as GameManager;
             
                 if (_instance == null)
                 {
                     Debug.Log("No object");
 
                     GameObject go = new GameObject("[GameManager]");
                     DontDestroyOnLoad(go);
                     _instance = go.AddComponent<GameManager>();
                 }
             }
             return _instance;
         }
     }
 
     void Awake () {
 
         Debug.Log("Waking up");
     }
 }




Comment
Add comment · Show 1
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 CruS · Oct 15, 2015 at 04:47 AM 0
Share

I added a Debug.Log line when OnDestroy(). When changing scene in case 3, which mean the game manager was left in the hierarchy from the previous run, it does get destroyed.

The question is narrowed down to this, still not sure why.

0 Replies

· Add your reply
  • Sort: 

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

30 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

Related Questions

Set location Rotation when changing a scene 3 Answers

save variable from previous scene 2 Answers

Destroyed object in DontDestroyOnLoad does not recreate in its main scene 0 Answers

How can I destroy a GameObject after a scene is loaded? 1 Answer

Can I create a scene DontDestroyOnLoad in editing mode?? 0 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