// QuitGameTrigger.cs using UnityEngine; public class QuitGameTrigger : MonoBehaviour { [Tooltip("The tag of the player GameObject")] public string playerTag = "Player"; [Tooltip("How many seconds to wait before quitting (gives time for a sound/fade)")] public float quitDelay = 0.5f; private bool isQuitting = false; // Prevents multiple triggers // This function is called when a 2D collider enters this trigger private void OnTriggerEnter2D(Collider2D other) { // Check if the object that entered is the player and we aren't already quitting if (other.CompareTag(playerTag) && !isQuitting) { isQuitting = true; // Set flag to true Debug.Log("Player entered Quit Trigger. Quitting application..."); // You could play a sound effect here // AudioSource.PlayClipAtPoint(quitSound, transform.position); // Call the QuitGame method after a short delay Invoke(nameof(QuitGame), quitDelay); } } private void QuitGame() { // This part is a bit special because Application.Quit() works differently // in the editor vs. a built game. #if UNITY_EDITOR // If running in the Unity Editor, stop the play mode UnityEditor.EditorApplication.isPlaying = false; #else // If running in a built game (like a .exe), quit the application Application.Quit(); #endif } }