cannot load scene: invalid scene name (empty string)

Hello all, I’m sure this one will be super easy to fix but I can’t for the life of me find the problem.

I have a toggle on my options menu to display or not display the tutorial which changes “Toggle_Changed” in the code below, and when play is clicked on the main menu we run “SceneTransitioner()” This code works if you do NOT check the box, that is choose NOT to display the tutorial, but if you leave it checked I get the error in the title, as well as “and invalid build index -1”. I DID add the tutorial scene “Tutorial01” to the build settings, and it does exist though it’s an empty scene with just a camera for now.

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


public class TitleMenu : MonoBehaviour
{
    public string startScene = "Tutorial01";
   
    public void SceneTransitioner()
    {
        SceneManager.LoadScene(startScene);
    }

    public void Toggle_Changed(bool newValue)
    {
        if (newValue == true)
        {
            startScene = "Tutorial01";
        }
        else
        {
            startScene = "Level01";
        }
    }
}

Before calling the LoadScene in line 14 above, print the value of startScene with Debug.Log()… you might discover something interesting. Maybe there are two versions of this script in the game, only one of which you intend.

I’m guessing that startScene doesn’t contain the value you think it does. When you first wrote this script, did you have this line?

public string startScene;

and then add the = “Tutorial01” later?
When you create a public variable, as soon as Unity compiles the script, it serializes (saves) that default value to the instance of your script that’s attached to your GameObject. Any future changes to the default value (e.g. adding = “Tutorial01” after the fact) will be ignored unless you removed and re-add the script to the object.

Look at your script in the inspector and check out the value of startScene. It’s probably blank.

I tried typing in the title manually, on the TitleMenu script component attached to my canvas, of which there is only one instance, and Debug.Log(startScene) spits out “Tutorial01”,
but, “Scene “Tutorial01” couldn’t be loaded because it has not been added to the build settings or the AssetBundle has not been loaded” is the error I get when trying that despite the screenshot I have attached.

5927381--633755--Screen Shot 2020-06-01 at 3.48.20 PM.png

Yes, I added the “= “Tutorial01”;” after, but see my above reply to another user please.

OK, that’s odd. Is it possible there’s some whitespace after “Tutorial01”?

Also, try “Scenes/Tutorial01” just in case. The docs say it should work with either but it doesn’t hurt to check.

No white space, using the path didn’t work either. Thanks for all the help everyone, I’ll keep playing with it.

Fixed with an editor restart, I changed projects and changed back and it works. Thanks everyone for your help!

1 Like