- Home /
 
Only One Object
So, I have this game manager in my 1st scene. I used the DontDestroyOnLoad () method to ensure that it wouldn't be destroy. However, if I go to my 2nd scene, and then go back to the 1st scene again, there will be 2 game managers. Can someone help me to solve this problem plz??? C# script prefer. Thank you.
Answer by RemiR · Nov 10, 2014 at 12:45 PM
Use singleton pattern, it's a good practice : http://unitypatterns.com/singletons/ (check The Persistent Singleton section).
Answer by GameVortex · Nov 10, 2014 at 12:08 PM
There are many solutions to this. They all depend on what your specific setup is, and what would work best for you.
You can have the GameManager script check on start if there already exists a GameManager and have it destroy itself if there is.
You can create a new GameManager scene which is the first scene to be loaded which you never load again. The GameManager scene then loads your first level.
You can make the GameManager a sort of singleton and have it create itself the first time something tries to use it. This way it is not already in the scene and will not be loaded each time the scene loads.
Your answer
 
             Follow this Question
Related Questions
gun manager how to 2 Answers
How to get variables from other Scripts on trigger enter? 1 Answer
Game Manager - simply use static variables? 0 Answers
isLocalPlayer is ReadOnly I can not set an object as a local player?(NetworkManager) 0 Answers
Unity 2018.3.11f1 Failed to start the Unity Package Manager local server process 1 Answer