Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 NeatWolf · Dec 13, 2016 at 09:13 AM · scene-loadingscene-switchingeventsscene changeorder-of-execution

new SceneManager (Unity 5.4.2f2 or newer) events. When do they fire in the event execution order?

Hi,

I have several DontDestroyOnLoad objects moving among scenes, and I'd like to use the new SceneManager events to hook on scene laoded/unloaded/changed.

Yet, also for future reference, I'd like to know:

When do:

  1. SceneManager.activeSceneChanged

  2. SceneManager.sceneLoaded

  3. SceneManager.sceneUnloaded

happen in this (outdated, since it's still using the now obsolete OnLevelWasLoaded) chart?

https://docs.unity3d.com/Manual/ExecutionOrder.html

Do the 2 and 3 get fired also when simply switching the active scene?

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 fafase · Dec 13, 2016 at 11:23 AM 0
Share

Have you tried to register to those delegates to see when they trigger?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by JacksonHLBC · Aug 04, 2021 at 12:33 AM

I know this is an old post, but I was interested and thought I'd share my investigation since this appeared high up in the google search algorithm.


TLDR

  • For Simple Scene Loading Mode: The events sceneLoaded and activeSceneChanged occur between Awake/OnEnable and Start, and activeSceneChanged comes before sceneLoaded.

  • For Additive Scene Loading Mode: sceneLoaded occurs between Awake/OnEnable and Start, and activeSceneChanged occurs later once SetActiveScene is called (since the Scene must be loaded before this is allowed).

  • For Unloading: sceneUnloaded occurs after OnDisable and OnDestroy, but before the next Scene has been loaded (See end of this post for consequences of the event occurring after disable/destroy). It is not called when the game (or play mode) is closed/last Scene unloaded.


    What Unity says in the documentation

  1. sceneLoaded -- The documentation is clear that sceneLoaded event triggers after Awake + OnEnable, and before Start.

  2. sceneUnloaded -- This documentation is for the most part self-explanatory, the scene won't unload until it has loaded, so it's safe to assume it comes after Awake, OnEnable, Start, etc. It would also make sense for sceneUnloaded to be called after all disable/destroy calls are complete, though this isn't clear.

  3. activeSceneChanged -- This documentation is the least clear in my opinion as it adds the event on Start and the ChangeScene method calls SetActiveScene explicitly right after LoadScene. This implies that the activeSceneChanged event is something to trigger during the process of changing scenes. Additionally, the SetActiveScene call does not make sense here as it is meant for switching active status of two additive scenes (LoadScene by default loads a Simple Scene) and would fail either way since the scene hasn't actually loaded yet.


What I tested locally

Note for these tests I ran on Unity 2019.4.21f1: For my tests, I had buttons to switch between two identical scenes. Each Scene had a button to switch instantly (LoadScene(scene2Name)) and a button to switch gradually like a loading screen (LoadScene(scene2Name, LoadSceneMode.Additive) + SetActiveScene(scene2) + UnloadScene(scene1)). Additionally I had one script in each scene that printed debug lines for each of the following callbacks: Awake, OnEnable, Start, OnDisable, OnDestroy, sceneLoaded, activeSceneChanged, sceneUnloaded

TLDR section has my results from this test.


Additional notes about sceneUnloaded

Because sceneUnloaded is triggered after OnDisable/OnDestroy this can disrupt how one typically subscribes to events. If you're like me, your typical strategy looks like the following:

 using UnityEngine;
 
 public class MyScript : MonoBehaviour
 {
     void OnEnable()
     {
         someEvent += OnEventTriggered;
     }
 
     void OnDisable()
     {
         // Allows one to stop the callback from happening by simply disabling the component.
         someEvent -= OnEventTriggered;
     }
 
     void OnEventTriggered()
     {
         // do stuff
     }
 }

Unfortunately, because sceneUnloaded occurs after OnDisable (and similarly OnDestroy), this will unsubscribe before the event triggers and miss the event call. And if you never unsubscribe from the event, then the method will still get callbacks when the next scene is unloaded. Additionally, if you try to use resources local to the component, most will cause NullPointerExceptions since you're accessing data from a destroyed gameobject :( (I didn't check the extent to which resources are inaccessible after OnDestroy, but it is safer to assume none of them are).


A couple ideas to work around this:

  1. Use a ScriptableObject instead of a MonoBehaviour: ScriptableObjects are scene agnostic and can be set to not unload when unused (omni-present callbacks). If you're dealing with data that isn't tied to a specific gameobject, then it's likely better suited for a more global entity to manage.

  2. Interact with the sceneUnloaded event with additive scenes: This problem only exists if the object that is subscribed is the one that is unloaded. Instead you could write the logic into a separate scene that is loaded additively. This way the component will be around when the previous scene is unloaded.

  3. Unsubscribe as part of the callback itself: I feel this is best explained via code.

...

 using UnityEngine;
 using UnityEngine.SceneManagement;
 
 public class MyScript : MonoBehaviour
 {
     void Awake()
     {
         // do stuff
         SceneManager.sceneUnloaded += OnSceneUnloaded;
     }
 
     void OnSceneUnloaded(Scene scene)
     {
         // do stuff
         if (this == null)
             SceneManager.sceneUnloaded -= OnSceneUnloaded;
     }
 }

Note: I use "this == null" so that this code is compatible with unloading additive scenes (don't subscribe unless you are being unloaded).

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

60 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

Related Questions

Help with error message "Overwriting the same path as another open scene is not allowed" 0 Answers

,Enums not being passed between scripts properly 1 Answer

Switching between several levels 1 Answer

SceneManager.LoadScene not working with button 1 Answer

How Do I Link Different Scenes? 4 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