<aside> 📄 Use this script to switch scene when your player collides with an object.
</aside>
Note: Make sure that the object the script is attached to has a Collider component set as trigger. Also, create an empty GameObject tagged as "Player" and position it where the player character will be in the scene.
using UnityEngine;
using UnityEngine.SceneManagement;
public class SceneSwitchOnTouch : MonoBehaviour
{
public string nextSceneName; // Name of the scene to switch to
// OnTriggerEnter is called when the Collider other enters the trigger
void OnTriggerEnter(Collider other)
{
// Check if the object that entered the trigger is tagged as "Player"
if (other.CompareTag("Player"))
{
// Call a method to switch to the next scene
SwitchScene();
}
}
void SwitchScene()
{
// Check if the next scene name is not empty
if (!string.IsNullOrEmpty(nextSceneName))
{
// Load the next scene
SceneManager.LoadScene(nextSceneName);
}
else
{
// If nextSceneName is not set, print an error message
Debug.LogError("Next Scene Name is not set in the SceneSwitchOnTouch script.");
}
}
}