Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Bug You cannot load a scene from the OnUnloadEventCompleted callback.

Discussion in 'Netcode for GameObjects' started by hoesterey, Apr 13, 2022.

  1. hoesterey

    hoesterey

    Joined:
    Mar 19, 2010
    Posts:
    659
    From the OnUnloadEventComplete callback if you load a new scene the scene will not load. You must wait a frame before loading the new scene.

    Code (CSharp):
    1. private void SceneManager_OnUnloadEventCompleted(string sceneName, LoadSceneMode loadSceneMode, List<ulong> clientsCompleted, List<ulong> clientsTimedOut)
    2.         {
    3.             if (IsServer)
    4.             {
    5.                // this will NOT work
    6. NetworkManager.Singleton.SceneManager.LoadScene(m_ActiveEncounter, LoadSceneMode.Additive);
    7.  
    8.                 // it will work if you wait a frame.
    9.                 StartCoroutine(OnUnloadEventCompleted());
    10.             }
    11.         }
    12.  
    13. private IEnumerator OnUnloadEventCompleted()
    14.         {
    15.             yield return 0;
    16.             NetworkManager.Singleton.SceneManager.LoadScene(m_ActiveEncounter, LoadSceneMode.Additive);
    17. }
     
  2. Ishnubarak

    Ishnubarak

    Joined:
    Aug 28, 2019
    Posts:
    6
    hoesterey likes this.