Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by MibZ · Oct 02, 2012 at 09:48 PM · dontdestroyonload

DontDestroyOnLoad issue

In my menu scene I have an empty GameObject that uses DontDestroyOnLoad to maintain variables between scene loads, but every time the menu scene is loaded another copy of it is created and maintained.

I know that I could add code to another script in the menu scene to check and see if the object exists and if it doesn't instantiate a new one from a prefab, but I have a feeling that there should be functionality built into Unity to do this since the DontDestroyOnLoad function exists in the first place.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Statement · Oct 02, 2012 at 10:01 PM

There is no such functionality. You can either have a "boot loader scene", or you can write a simple check inside the Awake method of a "boot loader object". The following snippet shows how you can create a boot loader that instantiates an object if it hasn't already been instantiated. If you then make this BootLoader a prefab and have it in each scene, then you can rest assured that you will always get a prefab instantiated once. This prefab the boot loader instantiates could then hold your data.

 public class BootLoader : MonoBehavior {
     public GameObject prefab;
     static Object instance;
     void Awake () {
         if (!instance) { 
             instance = Instantiate (prefab);
             DontDestroyOnLoad (instance);
         }
         DestroyImmediate (gameObject);
     } 
 }
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 MibZ · Oct 02, 2012 at 10:15 PM 0
Share

$$anonymous$$y first idea for a workaround was something similar to this but not quite as pretty, but ultimately I want to be 100% sure that the object is maintained and adding more code adds more potential for error.

The solution I'm going to use is to have the object instantiated in its own scene that plays a splash animation so it loads before the main menu, with no option to reload the splash screen so it is impossible for more than one instance to exist.

Thank you for your prompt help!

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

10 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

Related Questions

DontDestroyOnLoad() does not seem to be working. 3 Answers

FindGameObjectsWithTag not finding a tagged object 4 Answers

Don'tDestroyOnLoad issue, please help :( 1 Answer

C# Unity 3d Don't Destroy Class On Load 1 Answer

Setting public GameObject to a different Prefab through code 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