Error Trying to unload scene with Network Behavior

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/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:

using System.Collections;
using System.Collections.Generic;
using Unity.Netcode;
using UnityEngine;
using UnityEngine.SceneManagement;

public class LoadLevel : NetworkBehaviour
{
    [SerializeField]
    private string m_SceneName;
    public Scene m_LoadedScene;
    // Update is called once per frame
   
    public void Start(){
        m_LoadedScene = SceneManager.GetActiveScene();
    }

    public override void OnNetworkSpawn(){
        NetworkManager.SceneManager.OnSceneEvent += SceneManager_OnSceneEvent;
    }

    public void NewLevel()
    {
        Debug.Log("NEW SCENE");
        NetworkManager.SceneManager.LoadScene("networked out 1", LoadSceneMode.Additive);
        StartCoroutine(UnloadScene());
    }

    private void CheckStatus(SceneEventProgressStatus status, bool isLoading = true)
    {
        var sceneEventAction = isLoading ? "load" : "unload";
        if (status != SceneEventProgressStatus.Started)
        {
            Debug.LogWarning($"Failed to {sceneEventAction} {m_SceneName} with" +
                $" a {nameof(SceneEventProgressStatus)}: {status}");
        }
    }

    private void SceneManager_OnSceneEvent(SceneEvent sceneEvent)
    {
        var clientOrServer = sceneEvent.ClientId == NetworkManager.ServerClientId ? "server" : "client";
        switch (sceneEvent.SceneEventType)
        {
            case SceneEventType.LoadComplete:
                {
                    // We want to handle this for only the server-side
                    if (sceneEvent.ClientId == NetworkManager.ServerClientId)
                    {
                        // *** IMPORTANT ***
                        // Keep track of the loaded scene, you need this to unload it
                        //m_LoadedScene = sceneEvent.Scene;
                        Debug.Log("Dont change Yet");
                    }
                    Debug.Log($"Loaded the {sceneEvent.SceneName} scene on " +
                        $"{clientOrServer}-({sceneEvent.ClientId}).");
                    break;
                }
            case SceneEventType.UnloadComplete:
                {
                    Debug.Log($"Unloaded the {sceneEvent.SceneName} scene on " +
                        $"{clientOrServer}-({sceneEvent.ClientId}).");
                    break;
                }
            case SceneEventType.LoadEventCompleted:
            case SceneEventType.UnloadEventCompleted:
                {
                    var loadUnload = sceneEvent.SceneEventType == SceneEventType.LoadEventCompleted ? "Load" : "Unload";
                    Debug.Log($"{loadUnload} event completed for the following client " +
                        $"identifiers:({sceneEvent.ClientsThatCompleted})");
                    if (sceneEvent.ClientsThatTimedOut.Count > 0)
                    {
                        Debug.LogWarning($"{loadUnload} event timed out for the following client " +
                            $"identifiers:({sceneEvent.ClientsThatTimedOut})");
                    }
                    break;
                }
        }
    }

    IEnumerator UnloadScene()
    {
        // Assure only the server calls this when the NetworkObject is
        // spawned and the scene is loaded.
        yield return new WaitForSeconds(1);

        // Unload the scene
        Debug.Log(m_LoadedScene.name);
        var status = NetworkManager.SceneManager.UnloadScene(m_LoadedScene);
        CheckStatus(status, false);
    }
}

FIX UPDATE

I changed this function

public void NewLevel()
    {
        Debug.Log("NEW SCENE");
        NetworkManager.SceneManager.LoadScene("networked out 1", LoadSceneMode.Additive);
        StartCoroutine(UnloadScene());
    }

to this

 public void NewLevel()
    {
        Debug.Log("NEW SCENE");
        NetworkManager.SceneManager.LoadScene("networked out 1", LoadSceneMode.Single);
    }

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.

1 Like

Thanks !! I was wondering why I had this error