• 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
Question by Orolo · Oct 28, 2016 at 09:50 PM · scenescene-loadingscene-switching

How to ensure that an additively loaded scene is active before it becomes interactive?

I'm using the following code to load a scene additively that's supposed to become the active scene:

 SceneManager.LoadScene(scene, LoadSceneMode.Additive);
 yield return null;
 SceneManager.SetActiveScene(SceneManager.GetSceneByName(scene));

(scene is a string containing the name of the scene)

problem is, the scene can contain buttons that should only be clickable once the scene is actually the active scene. the way it is, the scene starts inactive and then becomes active. i want the scene to only show up when it's already active.

any way to do that?

Comment

People who like this

0 Show 2
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 SomeGuy22 · Oct 29, 2016 at 12:48 AM 0
Share

Have you looked at the Scene Manager class? This might help if you want to see when a scene was loaded.

avatar image Orolo SomeGuy22 · Oct 29, 2016 at 09:30 AM 0
Share

edit:

have tested the delegate. it appears that unity is actually calling the sceneLoaded-delegate before the scene is loaded. hard to believe, but in this code, bla turns out to be false:

 private void OnSceneLoaded(Scene scene, LoadSceneMode mode) {
     Debug.Log("OnSceneLoaded: " + scene.name + " FrameCount: " + Time.frameCount);
     bool bla = SceneManager.SetActiveScene(scene);
     Debug.Log(bla);
 }

according to the documentaiton for SetActiveScene, it returns false when the scene isnt loaded yet.

1 Reply

  • Sort: 
avatar image
Best Answer

Answer by Orolo · Oct 31, 2016 at 03:16 PM

not super happy with it, but i've now done it this way:

changed my scene-loading-script from the three lines in the question to:

 SceneManager.LoadScene(scene, LoadSceneMode.Additive);
 yield return new WaitUntil(() => SceneManager.GetActiveScene().name == scene);

(the yield return because callers need to be able to wait until the loading is complete)

then made following script:

 using UnityEngine;
 using UnityEngine.SceneManagement;
 
 public class ActivateScene : MonoBehaviour {
     void Start() {
         bool wasLoaded = SceneManager.SetActiveScene(gameObject.scene);
         Debug.Assert(wasLoaded);
     }
 }

had to use Start because Awake and OnEnable are both called before scene is considered loaded.

added ActivateScene to Script Execution Order settings and put it before Default Time.

then of course, added an ActivateScene-component to every scene that needs to be auto-activated.

if anyone knows a better way, please let me know.

Comment
JairoGLoz
Silvermurk
Hanh
willianjohns
umairwasim

People who like this

5 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 JairoGLoz · Feb 09, 2018 at 12:59 AM 0
Share

Nice solution man! thanks a lot!

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Trying to access SceneManagement 1 Answer

Scene Loading takes over 3 minutes 1 Answer

Level Softlocks when loaded 1 Answer

How to make GameObjects not reset back to their original values when switching scenes. 1 Answer

Referencing a gameobject in another scene 1 Answer


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