Add public variable to set scene from Inspector

So, like you I don’t like hard-coded values. So I added a public variable for the scene name:

public class LoseCollider : MonoBehaviour
{
    public string LoseScene = "Game Over";

    private void OnTriggerEnter2D(Collider2D collision)
    {
        SceneManager.LoadScene(LoseScene);

    }
}

This works fine and I can put the text name into the field and it’s no longer hard coded. I then thought, well how about if I just drag the scene into the field and use the name parameter from the Scene object. So I tried:

public class LoseCollider : MonoBehaviour
{
    public Scene LoseSceneObj;

    private void OnTriggerEnter2D(Collider2D collision)
    {
        SceneManager.LoadScene(LoseScene.name);

    }
}

Well in the Inspector id doesn’t provide a way to drag in a scene object. Instead there is a Handle parameter with and int (defaulted to 0). My thinking is that this is the build handle index, but not sure.

So my question is, would I be able to set this up in a way to be able to drag the Scene object to the field in the Inspector to get access to that object?

Thank you,

Brett

I’m going over the course right now and I have an answer to your question.

Instead of using public Scene LoseSceneObj;, use public Object LoseSceneObj;.
Then you’ll be able to drag the scene object to the field in the inspector

…and did this work for you? I am going through courses again and will try again when I get back to it.

Yep
image

1 Like

Privacy & Terms