Hi, I wanted to load a game scene that I created via a button in a menu but when I press the start button, I get this error message :
Cannot load scene: Invalid scene name (empty string) and invalid build index -1
UnityEngine.SceneManagement.SceneManager:LoadScene(String)
Menu:StartGame(String) (at Assets/Script/Menu.cs:10)
UnityEngine.EventSystems.EventSystem:Update()
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class Menu : MonoBehaviour
{
public void StartGame(string Forêt)
{
SceneManager.LoadScene(Forêt);
}
public void ExitGame()
{
Application.Quit();
}
public void ReturnMenu(string MenuLordara)
{
SceneManager.LoadScene(MenuLordara);
}
}
Make sure the string being supplied to the method is the name of the level you’d like to load and not an empty string.
Also check: File > Build Settings > add scene to ‘Scenes In Build’ list.
As far as i know scenes are references by an ID [int] and when using a name [string] Unity does an internal lookup to find that ID, if a scene cant be found by that ID then it returns -1
You are passing an empty parameter. First, do this:
public class Menu : MonoBehaviour
{
public void LoadNewScene(string sceneName)
{
SceneManager.LoadScene(sceneName);
}
public void ExitGame()
{
Application.Quit();
}
}
Now add the “LoadNewScene” method to your button’s “OnClick” event and write the name of the scene there. Make sure that you added your scenes in the build settings (File > Build Settings > Drag and drop your scenes assets to the “Scenes in Build” space) and that you wrote their names correctly.
I found ! It just miss the name of the level to load at the on click () ! Sorry for understanding if that’s what you were trying to say @Tobychappell
