Scene not loading in Unity 5.5 [SOLVED]

Is anyone else using Unity 5 ? I’m having an issue with the SceneManagement, The code and error are below.

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

public class LevelManager : MonoBehaviour {

public float autoLoadNextLevelAfter;
void Start()
{
    if (autoLoadNextLevelAfter > 0)
    {
        Invoke("LoadNextLevel", autoLoadNextLevelAfter);
    }
}
public void LoadLevel(string name)
{
    Debug.Log("New Level load: " + name);
    Scene s = SceneManager.GetSceneByName(name);
    Debug.Log("Scene is:");
    Debug.Log(s.name);
    Debug.Log(s.buildIndex);
    SceneManager.LoadScene(s.buildIndex);
    //Application.LoadLevel(name);
}
public void QuitRequest()
{
    Debug.Log("Quit requested");
    Application.Quit();
}
public void LoadNextLevel()
{
    SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
}

}

and the error that I’m getting is below.
Cannot load scene: Invalid scene name (empty string) and invalid build index -1

The name is coming in correctly but when I call GetSceneByName returns null

Thank you in advance!!

Changing my code to this made it work!

public void LoadLevel(string name)
{
SceneManager.LoadScene(name);
}

1 Like

Have you added your scenes to the build settings? =>
File -> Build Settings -> Scenes in Build?

This needed to access the scenes via the buildIndex

BR
Andreas

2 Likes

@andreas yes I place my scenes in the build settings. It’s working now when I passed the scene name to the SceneManager.LoadScene() function.

1 Like

My solution is: public void PlayGame() { int lvl = (int)(SceneManager.GetActiveScene().buildIndex + 1f); SceneManager.LoadScene(lvl); }

Privacy & Terms