Reload Scene when colliding with an object
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.
https://www.youtube.com/watch?v=Pe2qwTuT0P0&ab\_channel=LearnICTNow
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