Hi, I have problem with switching to new scene in Unity. According to my project, when I click “Play”, it should swich to new scene, but the only thing I get is “Cannot load scene: Invalid scene name (empty string) and invalid build index -1” error. I’ve tried everything, I surrender. Does anybody have a solution to this problem?
(Maybe there’s some problem with build settings, but I have no idea)
(I’m a noob in scripting, I know :P)
Plz help me, thank you in advance!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class LevelManager : MonoBehaviour
{
public void LoadLevel(string LevelToLoad)
{
Debug.Log("Level load requested for: " + LevelToLoad);
SceneManager.LoadScene(sceneName: LevelToLoad);
}
}
I’ve corrected it and it still says: “Cannot load scene: Invalid scene name (empty string) and invalid build index -1
UnityEngine.SceneManagement.SceneManager:LoadScene(String)”.
Well, the error says empty string or invalid build index. I see you printing out the value in a debug, are you sure it matches? Should be case sensitive also.
When I print the string it just returns nothing. I also don’t think it’s about the case sensivity, I’ve tried with many string and scene names. Probably the problem is in the build settings, but everything seems ok, I have really no idea, what may be wrong. Could someone make and send a simple project with scene switching? I’d like to compare it with mine.
If you print the string and the string is empty…well, that’s the problem. You need to make sure the string you pass in has a value.
No need to send you anything. That’s pretty straight forward. Your LevelToLoad is an empty string, just as you said. So make sure when you call the method, you are passing in a value.
Why are you reviving a 3 year old post to put inaccurate information within the context of the post? LevelToLoad is a string variable. You don’t need to add quotes around it when passing it to LoadScene. Doing so would treat the variable LevelToLoad as a string which would then try to load a scene called “LevelToLoad”
Quotes would only be needed if you were actually using the string, like “Level 1”. But since the method is being called most likely from a button press where LevelToLoad is set within the OnClick on the button, the string is put there, so quotes would not be needed within the context of this script.