using UnityEngine; using UnityEngine.UI; using TMPro; using System.Collections; using UnityEngine.SceneManagement; // <-- 1. ADDED THIS LINE for scene management public class PlayerInventory : MonoBehaviour { public static PlayerInventory Instance { get; private set; } public bool hasCollectedPot { get; private set; } = false; // --- UI REFERENCES --- [Header("Pot Collection UI")] public GameObject potIconUIObject; [Header("General Notification UI")] public TextMeshProUGUI notificationTextUI; public float notificationDisplayTime = 2.5f; [Header("Persistent Status UI")] public TextMeshProUGUI potStatusText; private Coroutine currentNotificationCoroutine; void Awake() { if (Instance == null) { Instance = this; DontDestroyOnLoad(gameObject); } else if (Instance != this) { Destroy(gameObject); return; } if (potIconUIObject) potIconUIObject.SetActive(false); if (notificationTextUI) notificationTextUI.gameObject.SetActive(false); } // --- 2. ADDED SCENE LOAD MANAGEMENT --- // This is called when the object becomes active. We subscribe our method to the sceneLoaded event. void OnEnable() { SceneManager.sceneLoaded += OnSceneLoaded; } // This is called when the object is deactivated or destroyed. We unsubscribe to prevent errors. void OnDisable() { SceneManager.sceneLoaded -= OnSceneLoaded; } // --- 3. ADDED THE MASTER FIX METHOD --- // This method is now called automatically by Unity every time a new scene has finished loading. // It acts as a "janitor" to clean up and reset the player's state. void OnSceneLoaded(Scene scene, LoadSceneMode mode) { Debug.Log("Scene '" + scene.name + "' loaded. Running state reset for player."); // FIX 1: Reset player's position to the new scene's spawn point. GameObject spawnPoint = GameObject.Find("SpawnPoint"); if (spawnPoint != null) { // 'gameObject' refers to the Player object this script is attached to. gameObject.transform.position = spawnPoint.transform.position; Debug.Log("Player position has been reset to the SpawnPoint."); } else { Debug.LogWarning("No 'SpawnPoint' found in scene '" + scene.name + "'. Player position was not reset."); } // FIX 2: Reset and hide the notification UI. if (notificationTextUI != null) { // If a notification was in the middle of its countdown from the previous level, stop it. if (currentNotificationCoroutine != null) { StopCoroutine(currentNotificationCoroutine); currentNotificationCoroutine = null; } // Forcefully hide the notification text object. notificationTextUI.gameObject.SetActive(false); Debug.Log("Notification UI has been reset."); } } void Start() { UpdatePotStatusText(); } void UpdatePotStatusText() { if (potStatusText == null) return; if (hasCollectedPot) { potStatusText.text = "Pot picked up: 1/1"; } else { potStatusText.text = "Pot picked up: 0/1"; } } public void CollectPot() { if (hasCollectedPot) return; hasCollectedPot = true; Debug.Log("Pot collected!"); UpdatePotStatusText(); ShowTemporaryNotification("Pot Picked Up!"); if (potIconUIObject) potIconUIObject.SetActive(true); } public void ResetPotStatus() { hasCollectedPot = false; Debug.Log("Pot status reset."); UpdatePotStatusText(); if (potIconUIObject) potIconUIObject.SetActive(false); if (notificationTextUI && notificationTextUI.gameObject.activeSelf) { if (currentNotificationCoroutine != null) { StopCoroutine(currentNotificationCoroutine); currentNotificationCoroutine = null; } notificationTextUI.gameObject.SetActive(false); } } public bool HasPot() { return hasCollectedPot; } public void ShowTemporaryNotification(string message) { if (notificationTextUI == null) { Debug.LogWarning("NotificationTextUI not assigned in PlayerInventory. Cannot display message: " + message); return; } if (currentNotificationCoroutine != null) { StopCoroutine(currentNotificationCoroutine); } notificationTextUI.text = message; notificationTextUI.gameObject.SetActive(true); currentNotificationCoroutine = StartCoroutine(HideNotificationAfterDelay(notificationDisplayTime)); } private IEnumerator HideNotificationAfterDelay(float delay) { yield return new WaitForSeconds(delay); if (notificationTextUI != null) { notificationTextUI.gameObject.SetActive(false); } currentNotificationCoroutine = null; } }