using UnityEngine; using UnityEngine.SceneManagement; public class DoorTrigger : MonoBehaviour { private void OnTriggerEnter2D(Collider2D collision) { if (!collision.CompareTag("Player")) return; if (!KeyManager.hasKey) { Debug.Log("Player tried to enter door WITHOUT key"); return; } KeyManager.hasKey = false; int currentIndex = SceneManager.GetActiveScene().buildIndex; int nextIndex = currentIndex + 1; if (nextIndex < SceneManager.sceneCountInBuildSettings) { string nextSceneName = SceneUtility.GetScenePathByBuildIndex(nextIndex); Debug.Log("Loading next scene by build index: " + nextSceneName); SceneManager.LoadScene(nextIndex); } else { Debug.Log("No more scenes to load. End of game?"); // Optional: SceneManager.LoadScene("MainMenu"); } } }