Home > database >  Why when using public Scene i don't see the variable in the inspector?
Why when using public Scene i don't see the variable in the inspector?

Time:06-19

[Header("Levels To Load")]
public Scene _newGameLevel;
private string levelToLoad;

And in the inspector :

There is no field to drag or select a scene. The New Game Level is just a static text.

New Game Level

Before that i used string instead Scene and then i had to type the scene name in text to load. but now i want instead typing each time the scene to load just to drag a scene to the inspector and get the name of it.

This is how i use it :

StartCoroutine(sceneFader.FadeAndLoadScene(SceneFader.FadeDirection.In, _newGameLevel.name));

but there is no Scene field to drag any scene to it. This screenshot show my scenes and the scene i want to drag to the script is the Starter.

Scenes

CodePudding user response:

Scene Assets cannot be serialized in the Inspector.

But I can give you one idea to solve the problem you are facing.

* IDEA

Instead of directly assigning a Scene Asset to the Inspector, you only need to assign a string corresponding to the Scene.

* SOLVED

I solved it in the following way. However, this method requires a paid package called Odin Inspector.

public class Test : MonoBehaviour
{
  [ValueDropdown("SelectScene", DropdownTitle = "Scene Selection")]
  public string sceneName;

  private static IEnumerable SelectScene()
  {
    var filesPath = Directory.GetFiles("Assets/Scenes");
    var fileNameList = filesPath
      .Select(Path.GetFileName)
      .Select(file => file.Split(".")[0])
      .Distinct()
      .ToList();

    return fileNameList;
  }
}

enter image description here

You can use it like this :

StartCoroutine(
    sceneFader.FadeAndLoadScene(SceneFader.FadeDirection.In, sceneName));

There will be other ways besides this one. But, that's all I can do with my abilities.

Hope your problem is solved :)

  • Related