• 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 BagelDesu · Oct 08, 2018 at 12:59 AM · scene-loadingscene-changeonenableondisable

OnSceneChange for Don'tDestroyOnLoad GameObjects?

Hi there Unity sages, I'm a student programmer that's currently stuck in a very weird situation.

The rundown of what my code is supposed to be doing is that:

  • Don'tDestroyOnLoad
  • Whenever specific scenes load(e.x Main) it's supposed to make references to gameobjects in the main scene from DDOL
  • Use the functions from those Gameobjects.

What I have currently done is made a preloader Scene to instantiate my Managers to hold the references and made a script for it to get references from the main scene when the scene is loaded. I ac$$anonymous$$eved that by using OnEnable and OnDisable and then doing SceneContainer.GetComponentsInC$$anonymous$$ldren(true); and then sorting it all out. It was working however out of the blue it just started freaking out and stop working. The OnEnable/Disable is now being randomly called(sometimes not even being called) and on first run OnEnable/Disable happen before OnAwake in the scene. I have been frustrated because of t$$anonymous$$s problem for 2 days now and i've run out of ideas. Sorry for the long read! Thanks for the help!

alt text Notice how SceneLoadCheck is called before Awake does.

   /// <summary>
     /// 
     /// Scene Swap Interaction
     /// 
     /// Resets the required References whenever the scene changes.
     /// 
     /// 
     /// </summary>
     private void OnEnable()
     {
         SceneManager.sceneLoaded += SceneHasLoaded;
     }
 
     private void OnDisable()
     {
         SceneManager.sceneLoaded -= SceneHasLoaded;
     }
 
     void SceneHasLoaded(Scene scene, LoadSceneMode mode)
     {
         Debug.Log("Scene Load Check");
 
         Debug.Log(SceneManager.GetActiveScene().name);
 
         if (SceneManager.GetActiveScene().name == "_PreLoad")
         {
             SceneManager.SetActiveScene(SceneManager.GetSceneByName("Pre-Alpha 2.1.0"));
         }
 
         
         StartCoroutine(StartUp(scene));
 
     }

The Code in question. StartUp(scene); is where I reference my objects
alt text EDIT: I've been running more tests and it seems that my Main scene is being loaded more than once? I did SceneManager.sceneCount and it returned 4 active scenes when it should only be 2. I don't know if t$$anonymous$$s might be relevant or not but it's the only result i've had in 5 hrs


5217b4aecf943c0ce6c1a2d845da0365.png (59.0 kB)
073f400663fccbffafc7e0e1fec26402.png (14.5 kB)
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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

94 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 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 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 avatar image avatar image avatar image avatar image

Related Questions

would Unity disable gameobject temporary and automatically when loading a big assetbundle? 0 Answers

Re-loading a scene but on the background older scenes are displayed 1 Answer

Is there any ideas to load Unity scene asynchronously? 1 Answer

Using parameters of activeSceneChanged? 0 Answers

Unity Scene loading problem (slow loading until it freezes) 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