using UnityEngine; using UnityEngine.SceneManagement; // finsih flag public class FinishLine : MonoBehaviour { public GameObject winPanel; // Optional UI panel void OnTriggerEnter2D(Collider2D other) { PlayerInventory player = other.GetComponent(); if (player != null && player.hasKey) { Debug.Log("You finished the level!"); if (winPanel != null) { winPanel.SetActive(true); Time.timeScale = 0f; } // Automatically load next scene by build index int currentSceneIndex = SceneManager.GetActiveScene().buildIndex; int nextSceneIndex = currentSceneIndex + 1; if (nextSceneIndex < SceneManager.sceneCountInBuildSettings) { SceneManager.LoadScene(nextSceneIndex); } else { Debug.Log("All levels complete!"); // Optional: Load a win screen or restart // SceneManager.LoadScene("MainMenu"); } } else { Debug.Log("You need a key to finish!"); } } }