0 of 16 lessons complete (0%)
Essential Activities

Reload Scene when colliding with an object

You don’t have access to this lesson
Please register or sign in to access the course content.
Length: 10 minutes

This guide will show how to reload a scene (or load a scene of your choice) when the player collides with an object.

You will need to have a basic level created with a player character that can move. Make a level here or a player here.

Create a level and place a player object on it.

We are going to make a trigger that when the player lands on an object. In the example below the green ground plane the trigger will reload the scene.

Make sure that the player object has the Player tag assigned to it.

Add a plane and resize it so that it is larger than the level and below the level.

On the object change the collider to be a trigger (if it’s a plane check convex as well).

Make a new script called ReloadScene or similar.

Add the SceneManagement package as shown on line 4.

Then add a trigger method OnTriggerEnter. This takes a parameter of type Collider, we have created a parameter named col.

Inside the method add an if statement to compare the tag on the object that was collided with. We are going to check if this was the Player tag.

If it was we will run the code on line 10. This uses the SceneManager to load a scene. In this case we get the Active scene and it’s name to reload the current scene.

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

public class ReloadScene : MonoBehaviour
{
    void OnTriggerEnter(Collider col) {
        if(col.CompareTag("Player")) {
            SceneManager.LoadScene(SceneManager.GetActiveScene().name);
        }
    }
}

Attach this script to the object that you want to use to reset the level.

In this example it is the Plane object

When the player contacts / collides with the green area the level will reload