Creating a Game Over after a Trigger/Event

Hello everyone! Loads of times I have found very useful answerered questions, this time, my first time writing here, I need a little more help. I am sorry in advance for not being a pro with unity.
I would like some help on switching from scene to scene after an even happened in my game (I am using C# for my scripts).

So, I am making a “demo” videogame for a university project. I have made a Main Menu scene and a Game scene. This demo ends with entering a particular room with a Trigger Collider and when you hit this collider, a video starts playing.

My video player code is this one:

    public GameObject videoPlayer;
    public int timeToStop;

    // Use this for initialization
    void Start()

    // Update is called once per frame
    void OnTriggerEnter(Collider player)

        if (player.gameObject.tag == "Player")
            Destroy(videoPlayer, timeToStop);

then it destroyes the object and the game starts again from where I left.
I would like to completely change scene (so I would make a Game Over scene) or I would like to go back to my Main Menu.

I have a code that makes me switch between Main Menu and Game:

public void PlayGame()
        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);

But this happens when I hit the Play button on the Main Menu. Now I would like to change scene suddenly after the event happened, so it won’t go through the game again.

Sorry for the long explanation and thank you even for reading this far!

Good day.

You are very close. As you know, all scenes have a name ,which is a string variable, and also have a int index number (the order you set them in the build window).

You can use LoadScene with the name or with the index, so if your scene with index number 2 is called “IceLevel” , this 2 lines do exactly the same:


Now, lets see what you do in your code. You are changing the level by its index, but instead of writing the number, you do this:


wich gives you the number of the current scene, so after adding 1 more, your final LoadScene command Load the next scene in your scene list.

If you want to control what to load then, just use the Scene name to load what you really need.

TRy it, and I’m sure you will get it!

Good luck! :smiley: