using UnityEngine; using UnityEngine.SceneManagement; // Required for scene management using TMPro; // Required if using TextMeshPro for the message public class RestartBox : MonoBehaviour { public string playerTag = "Player"; // Tag your player GameObject with "Player" public string restartMessage = "Level Restarted!"; public float messageDisplayTime = 2.5f; // Assign these in the Inspector if you want a UI message public TextMeshProUGUI notificationTextUI; // Assign your TextMeshPro UI element here private static bool isRestarting = false; // Prevents multiple simultaneous restarts void Awake() { // Ensure the notification text is hidden initially if it's assigned if (notificationTextUI != null) { notificationTextUI.gameObject.SetActive(false); } } void OnCollisionEnter2D(Collision2D collision) { HandleCollision(collision.gameObject); } void OnTriggerEnter2D(Collider2D other) { HandleCollision(other.gameObject); } void HandleCollision(GameObject collidedObject) { if (isRestarting) return; // Already processing a restart if (collidedObject.CompareTag(playerTag)) { isRestarting = true; // Set flag to prevent re-entry Debug.Log("Player hit the restart box!"); // Show message if UI element is assigned if (notificationTextUI != null) { notificationTextUI.text = restartMessage; notificationTextUI.gameObject.SetActive(true); // The message will disappear when the scene reloads } else { Debug.Log(restartMessage); // Fallback to console log if no UI } // If you have a PlayerInventory or GameManager that needs resetting: if (PlayerInventory.Instance != null) { PlayerInventory.Instance.ResetPotStatus(); } // Add any other game state resets here (e.g., score, lives) // Reload the current scene after a short delay to allow message to be seen (optional) // Or reload immediately if you prefer Invoke(nameof(ReloadScene), 0.1f); // Short delay, adjust if needed } } void ReloadScene() { SceneManager.LoadScene(SceneManager.GetActiveScene().name); // Important: Reset the static flag AFTER the scene has started loading // to allow the next instance to work. We'll do this in Start/Awake of the next loaded scene // or rely on static variables being reset on domain reload (usually happens on scene load in editor). // For a more robust solution, you might have a GameManager reset this flag. // For simplicity now, we'll let it be reset by the scene reload. // If issues persist with the flag, manage it via a singleton GameManager. } // Call this from a GameManager when a new scene loads to ensure the flag is cleared // or rely on static bools resetting on scene load in editor. public static void ClearRestartingFlag() { isRestarting = false; } // Optional: Ensure the flag is cleared if this object is destroyed before scene reload void OnDestroy() { // This might be too late if scene reload is immediate. // isRestarting = false; } // It's good practice to ensure the flag is reset when a new scene loads. // If you have a UIManager or GameManager that's persistent or re-initializes: void Start() { ClearRestartingFlag(); // Ensure flag is cleared when this box instance is ready } }