Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Question NetworkManager.Singleton.SceneManager.LoadScene --> Not working!

Discussion in 'Multiplayer' started by GuirieSanchez, Nov 5, 2022.

  1. GuirieSanchez

    GuirieSanchez

    Joined:
    Oct 12, 2021
    Posts:
    320
    Hi, I'm trying to load a scene as a host in order for other clients to join the same scene as the host. In order to do so, I assign first the host by using this piece of code:
    NetworkManager.Singleton.StartHost();


    Next, I want to load the next scene not by using the
    SceneManager
    but by using the
    NetworkManager.Singleton.SceneManager
    instead since I read in this thread that that way clients can join the very same scene as the host.

    To do so, I previously stored the scene as
    gameScene = SceneManager.GetSceneByBuildIndex(1);
    , and I'm trying this piece of code:
    NetworkManager.Singleton.SceneManager.LoadScene(gameScene.name, LoadSceneMode.Single)
    ;
    however, I'm getting the following error
    The scene is in build list. In fact, I can load the scene pretty easily by using the
    SceneManager
    piece of code. Any idea of what could be causing this issue?
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    1,830
    apparently gameScene.name is an empty string or null
    check whether you actually get a or the correct scene from build index 1
     
    GuirieSanchez likes this.
  3. GuirieSanchez

    GuirieSanchez

    Joined:
    Oct 12, 2021
    Posts:
    320
    Thanks for replying. Indeed, when I check the scene with
    Debug.Log("gameScene.name: " + gameScene.name);
    the name seems to be null or empty, as nothing shows up on the log box. The way I'm retrieving the scene looks pretty straightforward to me, so I ran out of ideas about why I'm getting a null/empty scene.

    Here's the code I used to retrieve the scene:
    gameScene = SceneManager.GetSceneByBuildIndex(1);
    , which is the number 2 of the build.
    Here's also a picture of the build with the scenes in it:

    upload_2022-11-5_13-42-33.png
     
  4. GuirieSanchez

    GuirieSanchez

    Joined:
    Oct 12, 2021
    Posts:
    320
    Ok, apparently
    gameScene = SceneManager.GetSceneByBuildIndex(1);
    is not reliable. I used the following code instead:
    Code (CSharp):
    1. gameScenePath = SceneUtility.GetScenePathByBuildIndex(1);
    2.  
    3. string gameSceneName = System.IO.Path.GetFileNameWithoutExtension(gameScenePath);
    and now it works. It also works by using the path directly in this manner:
    NetworkManager.Singleton.SceneManager.LoadScene(gameScenePath, LoadSceneMode.Single);