Search Unity

Resolved Error Trying to unload scene with Network Behavior

Discussion in 'Multiplayer' started by jscott15, Jan 13, 2023.

  1. jscott15

    jscott15

    Joined:
    Jan 6, 2023
    Posts:
    2
    Error is as follows:
    UnloadScene internal error! TestLevelScene with handle -1648 is not within the internal scenes loaded dictionary!
    UnityEngine.Debug:LogError (object)
    Unity.Netcode.NetworkSceneManager:UnloadScene (UnityEngine.SceneManagement.Scene) (at Library/PackageCache/com.unity.netcode.gameobjects@1.2.0/Runtime/SceneManagement/NetworkSceneManager.cs:960)
    LoadLevel/<UnloadScene>d__7:MoveNext () (at Assets/Scripts/LoadLevel.cs:88)
    UnityEngine.SetupCoroutine:InvokeMoveNext (System.Collections.IEnumerator,intptr)

    I am trying to unload a scene I tried to follow the guide here but I am running into the error above.

    Code is as follows:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using Unity.Netcode;
    4. using UnityEngine;
    5. using UnityEngine.SceneManagement;
    6.  
    7. public class LoadLevel : NetworkBehaviour
    8. {
    9.     [SerializeField]
    10.     private string m_SceneName;
    11.     public Scene m_LoadedScene;
    12.     // Update is called once per frame
    13.    
    14.     public void Start(){
    15.         m_LoadedScene = SceneManager.GetActiveScene();
    16.     }
    17.  
    18.     public override void OnNetworkSpawn(){
    19.         NetworkManager.SceneManager.OnSceneEvent += SceneManager_OnSceneEvent;
    20.     }
    21.  
    22.     public void NewLevel()
    23.     {
    24.         Debug.Log("NEW SCENE");
    25.         NetworkManager.SceneManager.LoadScene("networked out 1", LoadSceneMode.Additive);
    26.         StartCoroutine(UnloadScene());
    27.     }
    28.  
    29.     private void CheckStatus(SceneEventProgressStatus status, bool isLoading = true)
    30.     {
    31.         var sceneEventAction = isLoading ? "load" : "unload";
    32.         if (status != SceneEventProgressStatus.Started)
    33.         {
    34.             Debug.LogWarning($"Failed to {sceneEventAction} {m_SceneName} with" +
    35.                 $" a {nameof(SceneEventProgressStatus)}: {status}");
    36.         }
    37.     }
    38.  
    39.     private void SceneManager_OnSceneEvent(SceneEvent sceneEvent)
    40.     {
    41.         var clientOrServer = sceneEvent.ClientId == NetworkManager.ServerClientId ? "server" : "client";
    42.         switch (sceneEvent.SceneEventType)
    43.         {
    44.             case SceneEventType.LoadComplete:
    45.                 {
    46.                     // We want to handle this for only the server-side
    47.                     if (sceneEvent.ClientId == NetworkManager.ServerClientId)
    48.                     {
    49.                         // *** IMPORTANT ***
    50.                         // Keep track of the loaded scene, you need this to unload it
    51.                         //m_LoadedScene = sceneEvent.Scene;
    52.                         Debug.Log("Dont change Yet");
    53.                     }
    54.                     Debug.Log($"Loaded the {sceneEvent.SceneName} scene on " +
    55.                         $"{clientOrServer}-({sceneEvent.ClientId}).");
    56.                     break;
    57.                 }
    58.             case SceneEventType.UnloadComplete:
    59.                 {
    60.                     Debug.Log($"Unloaded the {sceneEvent.SceneName} scene on " +
    61.                         $"{clientOrServer}-({sceneEvent.ClientId}).");
    62.                     break;
    63.                 }
    64.             case SceneEventType.LoadEventCompleted:
    65.             case SceneEventType.UnloadEventCompleted:
    66.                 {
    67.                     var loadUnload = sceneEvent.SceneEventType == SceneEventType.LoadEventCompleted ? "Load" : "Unload";
    68.                     Debug.Log($"{loadUnload} event completed for the following client " +
    69.                         $"identifiers:({sceneEvent.ClientsThatCompleted})");
    70.                     if (sceneEvent.ClientsThatTimedOut.Count > 0)
    71.                     {
    72.                         Debug.LogWarning($"{loadUnload} event timed out for the following client " +
    73.                             $"identifiers:({sceneEvent.ClientsThatTimedOut})");
    74.                     }
    75.                     break;
    76.                 }
    77.         }
    78.     }
    79.  
    80.     IEnumerator UnloadScene()
    81.     {
    82.         // Assure only the server calls this when the NetworkObject is
    83.         // spawned and the scene is loaded.
    84.         yield return new WaitForSeconds(1);
    85.  
    86.         // Unload the scene
    87.         Debug.Log(m_LoadedScene.name);
    88.         var status = NetworkManager.SceneManager.UnloadScene(m_LoadedScene);
    89.         CheckStatus(status, false);
    90.     }
    91. }
    92.  
     
  2. jscott15

    jscott15

    Joined:
    Jan 6, 2023
    Posts:
    2
    FIX UPDATE

    I changed this function
    Code (CSharp):
    1. public void NewLevel()
    2.     {
    3.         Debug.Log("NEW SCENE");
    4.         NetworkManager.SceneManager.LoadScene("networked out 1", LoadSceneMode.Additive);
    5.         StartCoroutine(UnloadScene());
    6.     }
    to this

    Code (CSharp):
    1.  public void NewLevel()
    2.     {
    3.         Debug.Log("NEW SCENE");
    4.         NetworkManager.SceneManager.LoadScene("networked out 1", LoadSceneMode.Single);
    5.     }
     
  3. ahnjinmo

    ahnjinmo

    Joined:
    Jun 19, 2021
    Posts:
    1
    I figured out reason why that error occurs.

    It's because that TestLevelScene is not a scene which is loaded using NetworkSceneManager. Which means that the scene might be existing before StartHost or StartClient is called. Therefore, NetworkSceneManager cannot unload that scene because it didn't load that scene.
     
    Eyap likes this.
  4. Eyap

    Eyap

    Joined:
    Nov 17, 2017
    Posts:
    40
    Thanks !! I was wondering why I had this error