How to make an exit level trigger?

I want to make a platform that when walked on exits the level and brings you back to a level selection screen. Any thoughts/resources anyone has?

just make an empty object with is trigger checked then make a script that looks like this:

void OnTriggerEnter2D(Collider2D col)
    {
        if (col.gameObject.tag == "player")
        {
            SceneManager.LoadScene(sceneyouwanttoload);
        }
    }

make sure you write using UnityEngine.SceneManagement; and the player has a player tag

I tend to immitate techniques of famous games. In this case I would do it like Neverwinter Nights did it.

  • Add an empty gameobject to the scene

  • Attach a box collider to it, set it to trigger and adjust its dimensions to your liking

  • Now attach a script with the following code to the trigger object:

       public class Trigger : MonoBehaviour
     {
         public string sceneName;
     
         void OnTriggerEnter(Collider c)
         {
             if(c.tag == "Player")
                 UnityEngine.SceneManagement.SceneManager.LoadScene(sceneName);
         }
     }
    

If your player’s tag is “Player” and has a collider attached, it should work. Make sure to type the correct scene name into the Trigger Script inspector.

okay cool! that worked.

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


public class Trigger : MonoBehaviour
{
    public string sceneName;

    void OnTriggerEnter(Collider c)
    {
        if (c.tag == "Player")
            UnityEngine.SceneManagement.SceneManager.LoadScene("MainMenu");
    }

    /*public void OnTriggerEnter2D (Collider2D col)
{
    if (col.gameObject.tag == "player")
    {
        SceneManager.LoadScene("MainMenu");
    }
}
*/


}

so now i ran into another problem after that:

I am using the unity first person camera in this project and when i jump back into the “mainMenu” scene, i dont have control of the mouse to select anything… Thoughts?