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
0
Question by asdf123 · Aug 12, 2014 at 12:01 AM · gameobjectscenedontdestroyonload

Dont Destroy On Load - Exception

what i want to do is a bit hard to explain so i will try to be the maximum comprensible as i can.

i have 2 scenes.

and i have my menu manager that doesnt destroy on load with the command line: DontDestroyOnLoad();

and he pass from the scene 1 to scene 2

but when i pass from the scene 2 to scene 1 it duplicates, i just want to stay with the first menu manager.

there is a way to destroy the default menu manager from scene 1 when i pass from the scene 2 to scene 1 ?

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 Kiwasi · Aug 12, 2014 at 12:07 AM 0
Share

As always comprehension will be helped by proper capitalisation.

2 Replies

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

Answer by Kiwasi · Aug 12, 2014 at 12:25 AM

What you are after is the singleton pattern. Goes something like this

 public class MenuManager : MonoBehaviour {
 
     public static MenuManager instance;
 
     void Awake (){
         if(!instance){
             instance = this;
         } else {
             Destroy(gameObject);
         }
     }
 }



Comment
Add comment · Show 3 · 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 asdf123 · Aug 12, 2014 at 12:46 AM 0
Share

thats exaclty what i was looking for, thank you bro !

avatar image Kiwasi · Aug 12, 2014 at 12:53 AM 0
Share

You do realise this is identical to @rutter's answer?

avatar image asdf123 · Aug 12, 2014 at 01:09 AM 0
Share

i understood this code, but the other i didn't

avatar image
1

Answer by rutter · Aug 12, 2014 at 12:16 AM

You're looking for some variety of singleton pattern:

  • http://wiki.unity3d.com/index.php/Singleton

  • http://forum.unity3d.com/threads/unity-tips-the-singleton-pattern.144731/

  • http://answers.unity3d.com/questions/20949/singleton-implementation.html

  • http://unitypatterns.com/singletons/

Process is fairly simple, but important to get right:

  • You must keep track of a single, static "instance" of the class.

  • Once loaded, your object must check if there is currently any instance registered.

  • If there is not an instance, your object should become the new instance.

  • If there is an instance, you should resolve that; usually, you'll either destroy the new object, or destroy and replace the old one.

Code examples are fairly trivial if you understand enough programming to know what a static variable is. If not, time to look up some tutorials!

The links above should be helpful for additional explanation.

Comment
Add comment · Show 2 · 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 asdf123 · Aug 12, 2014 at 12:23 AM 0
Share

i think thats not what im looking for bro

avatar image Kiwasi · Aug 12, 2014 at 12:32 AM 0
Share

Beat me to it, and with a better answer too.

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

23 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

Related Questions

DontDestroyOnLoad Slowing Loading Down 0 Answers

GameObject::scene and DontDestroyOnLoad() 0 Answers

Keep gameObjects states between scenes without duplicating 1 Answer

I have an error, script should check for null or not destroy game objects 2 Answers

good resource about conserving memory when multiple levels share resources? 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