- Home /
 
Cannot load scene: Invalid scene name (empty string) and invalid build index -1 in Unity Test Runner?
I'm running unit tests using Unity's Test Runner. On the start of the test, I want it to load a scene that contains all the base stuff needed for my game to function but I can't get it to load a valid scene. Specifically I get the following error:
 Cannot load scene: Invalid scene name (empty string) and invalid build index -1
 UnityEngine.SceneManagement.SceneManager:LoadSceneAsync(String, LoadSceneMode)
 BBR2DTests.<LoadScene>d__1:MoveNext() (at Assets/Tests/BBRTestUtils.cs:16)
 UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr)
 
               I only have one scene in my build settings...

I'm loading it with the following code, using GetSceneByBuildIndex(0)
 public class NetworkedTest {
     public BBR2D.BBRNetworkManager netManager;
     public IEnumerator LoadScene() {//.
         Scene scene = SceneManager.GetSceneByBuildIndex(0);
         Debug.Log(scene + " " + scene.name + " " + scene.path);
         //This ouputs: "UnityEngine.SceneManagement.Scene "
         yield return SceneManager.LoadSceneAsync(scene.name, LoadSceneMode.Single);
         SceneManager.SetActiveScene(scene);
         netManager = GameObject.Find("Bootstrapper").GetComponent<BBR2D.BBRNetworkManager>();
     }
 }
 public class BBRConnection : NetworkedTest
 {
     [UnityTest]
     public IEnumerator isLocal() {
         //Arrange
         yield return LoadScene(); //Wait for the scene to load
         netManager.StartHost(); //Start a host
         //Act
          //Wait until there's a client (the host client) and it is ready
         yield return new WaitUntil(() => netManager.bbrConnections.Count > 0 && netManager.bbrConnections[0].isReady);
         //Assert
         //Local player should be local
         Assert.That(netManager.bbrConnections[0].isLocal);
     }
 }
 
               And to cover all my bases, here's my testing setup

I'm at my wits end, I've been trying to figure this out for 3 days...
I also was able to get these tests working already by recreating all the object/dependencies they require from scratch, so everything else should work, it's just the scene loading...
Am I missing something? Do scene changes no work in Unit tests?
Try Scene$$anonymous$$anager.LoadSceneAsync(0, LoadScene$$anonymous$$ode.Single);
Wow, that worked, thanks for the help! Any idea on why that worked?
Open build settings and see what the name of the scene is. Is it like Scenes/$$anonymous$$ain or something, if yes try putting the folder name behind scene.name like "Scenes/" + scene.name then give it a shot.
Your answer