// FinalLevelWinCondition.cs (Improved Version) using UnityEngine; using TMPro; public class FinalLevelWinCondition : MonoBehaviour { [Header("Win Condition")] public int potsRequired = 10; public string potTag = "FinalLevelPot"; [Header("UI Links")] public TextMeshProUGUI potCounterText; public GameObject winScreen; private int startingPotCount; private bool gameWon = false; void Start() { // Hide the win screen at the start. if (winScreen != null) { winScreen.SetActive(false); } else { Debug.LogError("Win Screen is not assigned in the Inspector!"); } // Find how many pots exist at the start of the level. startingPotCount = GameObject.FindGameObjectsWithTag(potTag).Length; UpdateUI(); } void Update() { // Stop checking after the game has been won. if (gameWon) return; UpdateUI(); CheckForWin(); } void UpdateUI() { if (potCounterText == null) return; int currentPotCount = GameObject.FindGameObjectsWithTag(potTag).Length; int potsCollected = startingPotCount - currentPotCount; potCounterText.text = "Pots: " + potsCollected + " / " + potsRequired; } void CheckForWin() { int currentPotCount = GameObject.FindGameObjectsWithTag(potTag).Length; int potsCollected = startingPotCount - currentPotCount; if (potsCollected >= potsRequired) { // --- This is the win sequence --- gameWon = true; // IMPORTANT: This stops the Update from running again. Debug.Log("Win condition met!"); // Show the win screen BEFORE pausing time. if (winScreen != null) { winScreen.SetActive(true); // If you use a CanvasGroup to fade UI, make sure its alpha is 1. CanvasGroup cg = winScreen.GetComponent(); if (cg != null) { cg.alpha = 1f; } } // NOW, pause the game. Time.timeScale = 0f; Debug.Log("YOU WON! GAME PAUSED."); } } }