Rookatu
Rookatu

Reputation: 1507

SceneManager.LoadScene(string) doesn't recognize scene name

I've seen similar questions all over the internet, but none seem to apply to my situation.

I have a very simple project with two scenes: MainMenuScene and OtherScene. OtherScene is nothing but a blank canvas, and MainMenuScene just has a button which is meant to be used to navigate to OtherScene. The Main Camera has the following script attached to it:

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

public class LaunchScene : MonoBehaviour {

    public void LaunchSceneOnButtonClick(int index)
    {
        SceneManager.LoadScene(index);
    }

    public void LaunchSceneOnButtonClick(string sceneName)
    {
        SceneManager.LoadScene(sceneName);
    }
}

and in the OnClick() of the button, this script is called. I have made sure to add both levels to the Build Settings.

If I call the version of the LanchSceneOnButtonClick which takes an integer argument (and specify the appropriate value in the OnClick() method) then the desired scene loads.

However, if I call the version which takes a string argument and use the exact name of the scene I want to load (and I've quadruple checked that it is indeed the exact string, that the string name has no spaces or hidden characters, and have copy pasted the scene name into the OnClick() argument field) the scene does not load and instead I get the error message

Scene "OtherScene" couldn't be loaded because it has not been added to the build settings or the AssetBundle has not been loaded.

Since using the scene index works, we know it was added to the Build Settings properly, so I reason that there is some issue with the scene name. But I've copied it exactly. I want to avoid using the full path of the scene in case I later decide to move scenes around to different folders, so what are my options here?

Thanks in advance.

EDIT:

Adding screenshots.

Scene in folder structure:

enter image description here

Scenes in build settings:

enter image description here

OnClick() input:

enter image description here

enter image description here

Keep in mind I've also tried the input to OnClick() with and without quotation marks.

Upvotes: 3

Views: 5247

Answers (2)

Nordic
Nordic

Reputation: 1

I had also the problem that

public void loadScene(string scene)
{
        SceneManager.LoadScene(scene);
}

did not work for me in the first place. What did I do? I'm saving the names of my scenes in a List (read from a file) and from that List I choose the scene I need.

But somehow the reading progress seems to give me some unwanted characters so the scene loading does not work as intended. When I added .Trim() it started to work.

For a better understanding, here's an example:

string s = sceneList[1].Trim();

this.loadScene(s);

public void loadScene(string scene)
{
        SceneManager.LoadScene(scene);
}

So make sure that you get rid of unwanted characters.

Upvotes: 0

Galandil
Galandil

Reputation: 4249

The only possible problems are these:

  • The string passed is different from the scene name; bear in mind, the string used in LoadScene is not case sensitive, i.e. you can use OtherScene, OTHERscene, etc.
  • The scene in the build settings is not active (i.e. with the checkmark on the left disabled).
  • The scene isn't present at all in the build settings.

The full path of the scene is irrelevant btw, you can have OtherScene inside any dir/subdir in the Assets, and you'll need only the name of the scene. You need the full path of the scene only if you have more than scene with the same name in different paths (and anyway you shouldn't, it's such an obvious terrible practice).

EDIT: You say that you tried even without " (as you should do, when entering string fields in the inspector you don't need them), what happens if you modify the method body to:

public void LaunchSceneOnButtonClick(string sceneName)
{
    SceneManager.LoadScene("OtherScene");
}

Try this and report back what happens please.

Upvotes: 3

Related Questions