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.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Cannot load scene: Invalid scene name (empty string) and invalid build index -1" Error

Discussion in 'Scripting' started by PavGos, May 23, 2018.

  1. PavGos

    PavGos

    Joined:
    May 23, 2018
    Posts:
    3
    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!



    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class LevelManager : MonoBehaviour
    7. {
    8.  
    9.     public void LoadLevel(string LevelToLoad)
    10.     {
    11.         Debug.Log("Level load requested for: " + LevelToLoad);
    12.         SceneManager.LoadScene(sceneName: LevelToLoad);
    13.     }
    14.  
    15. }
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,140
    Code (CSharp):
    1.  
    2. SceneManager.LoadScene(LevelToLoad);
    3.  
    As long as LevelToLoad is a valid scene name and that scene is in your Build settings, it will load (assuming no other errors)
     
  3. PavGos

    PavGos

    Joined:
    May 23, 2018
    Posts:
    3
    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)".

    Scene "LevelToLoad" is in the build settings.
     
  4. FMark92

    FMark92

    Joined:
    May 18, 2017
    Posts:
    1,244
    Are you sure you have the scene you're trying to load listed in player settings?

    Also show us play button settings.
     
  5. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,140
    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.
     
  6. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Just from the docs:
     
  7. PavGos

    PavGos

    Joined:
    May 23, 2018
    Posts:
    3
    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.
     
  8. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Post the script that calls your method to change scenes?
     
  9. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,140
    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.
     
    Kurt-Dekker likes this.
  10. firez05

    firez05

    Joined:
    May 20, 2021
    Posts:
    1
    it works like this:
    1. SceneManager.LoadScene("LevelToLoad");
    2. Just put this "" behind and infront of your level name because its a string
     
  11. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,140
    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.