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
4
Question by TheYokai · Oct 09, 2012 at 06:49 AM · c#startdontdestroyonloadsingletongame object

Preventing my game object from multiplying due to DontDestroyOnLoad()

I've been trying to implement a game object that is created at the "mainmenu" scene of my game that is intended to be omnipresent. This would allow me to easily manage things such as save data, user inventory, and state management all within one or multiple classes.

Currently, I have the Game_Manager class extending from MonoBehavior which runs the DontDestroyOnLoad() function inside it's Start() function. This works well and the Game_Manager game object exists even upon scene transitions (currently to a test scene), yet the current set up allows the game object to multiply itself as it is re-created upon loading the original main menu scene. In other words, whenever I switch back to the main menu level it will create another Game_Manager object.

One solution for this would be to make a "Initialize" scene which only has the function of introducing omnipresent game objects and will never be loaded again during runtime, but this solution seems less than ideal. Is there any way to prevent the game object from creating itself if one of a similar name already exists using a simple unity function / parameter or should I instead rely on creating a singleton gameobject? Feel free to throw in any other suggestions. I'm using C# for all scripting / programming in unity thus far.

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
18
Best Answer

Answer by whydoidoit · Oct 09, 2012 at 06:51 AM

Use a static variable to work out whether the object has already been loaded - destroy any copies if the static is set.

  public class MyPersistentThing : MonoBehaviour
 {
       public static MyPersistentThing Instance;
 
       void Awake()
       {
             If(Instance)
                 DestroyImmediate(gameObject);
             else
             {
                 DontDestroyOnLoad(gameObject);
                 Instance = this;
             }
       }
 }
Comment
Add comment · Show 5 · 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 TheYokai · Oct 09, 2012 at 07:04 AM 0
Share

Thanks, this seems to work.

avatar image Kerihobo · Dec 04, 2014 at 12:00 AM 0
Share

how did you get this to work?

I don't understand what this line is doing:

 public static $$anonymous$$yPersistentThing Instance;

none of those keywords are a "type"?

Is "$$anonymous$$yPersistentThing" the variable name? or is "Instance" the variable name? Assu$$anonymous$$g "Instance" is the variable name... what is "$$anonymous$$yPersistentThing"? GameObject?

avatar image mr_freedo Kerihobo · Sep 14, 2015 at 08:28 AM 1
Share

Change out $$anonymous$$yPersistentThing with the name of the class, and dont make a new class. $$anonymous$$y class is called Options$$anonymous$$enuScript so for me the code looks like this:

 public static Options$$anonymous$$enuScript Instance;
     
     void Awake()
     {
         if(Instance){
             DestroyImmediate(gameObject);
         }else
         {
             DontDestroyOnLoad(gameObject);
             Instance = this;
         }
     }

This worked for me so it should work for you as well, know its a long time since you posted, so this is most for other people having the same question.

avatar image RudyTheDev · Dec 04, 2014 at 12:26 AM 0
Share

$$anonymous$$yPersistentThing is the type, in this case it's a class ( public class $$anonymous$$yPersistentThing : $$anonymous$$onoBehaviour ). Instance is the variable/identifier name.

avatar image mr_freedo · Sep 14, 2015 at 08:23 AM 0
Share

Thanks a lot, This was realy helpful

avatar image
0

Answer by BillyBobBeavis · Mar 07, 2021 at 11:52 PM

My solution, which is more straightforward for numskulls like me, was to attach this script to any persistent GameObject. Seems to work. . . so far. This script should work unmodified with any GameObject that has a tag.

 public class DestroyDuplicateOnLoad : MonoBehaviour
 {
     private void Awake()
     {
         var otherPersistent = GameObject.FindWithTag(gameObject.tag);
         if (otherPersistent != null && otherPersistent != transform.gameObject)
             DestroyImmediate(transform.gameObject);
     }
 }
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

16 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

Related Questions

Initialising List array for use in a custom Editor 1 Answer

Singleton and DontDestroyOnLoad question 1 Answer

DontDestroyOnLoad Duplicating Objects When Scene Reloaded 1 Answer

Unity C# Singleton? 6 Answers

UNITY2D: How do I destroy previous object with Singleton Pattern? 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