using UnityEngine; using UnityEngine.SceneManagement; // Required for scene management public class NextLevelDoor : MonoBehaviour { // Public variable to set the name of the next scene in the Inspector public string nextSceneName; public string playerTag = "Player"; // You can set this in the Inspector if needed, defaults to "Player" // Optional: A message or sound to play if the door is locked // public AudioClip lockedSound; // public GameObject lockedMessageUI; // This function is called when another Collider2D enters the trigger attached to this GameObject private void OnTriggerEnter2D(Collider2D other) { // Check if the object that entered the trigger is tagged as "Player" if (other.CompareTag(playerTag)) { // NEW: Check with PlayerInventory if the pot has been collected if (PlayerInventory.Instance != null && PlayerInventory.Instance.HasPot()) { Debug.Log("Player entered the door with the pot. Loading next level: " + nextSceneName); LoadNextLevel(); } else { if (PlayerInventory.Instance == null) { Debug.LogError("PlayerInventory instance not found! Make sure a GameManager object with PlayerInventory script exists."); } else { Debug.Log("Door is locked! You need to find the pot first."); // Optional: Play a locked sound // if (lockedSound) AudioSource.PlayClipAtPoint(lockedSound, transform.position); // Optional: Show a UI message // if (lockedMessageUI) lockedMessageUI.SetActive(true); } } } } // Optional: If you have a locked message UI, you might want to hide it when the player leaves /* private void OnTriggerExit2D(Collider2D other) { if (other.CompareTag(playerTag)) { // if (lockedMessageUI) lockedMessageUI.SetActive(false); } } */ private void LoadNextLevel() { // Check if the scene name is not empty or null if (!string.IsNullOrEmpty(nextSceneName)) { // Load the scene specified by nextSceneName SceneManager.LoadScene(nextSceneName); } else { Debug.LogError("Next scene name is not set in the Inspector for the door object: " + gameObject.name); } } }