using UnityEngine; using UnityEngine.UI; public class FadeToBlack : MonoBehaviour { public float fadeDuration = 2f; private Image fadeImage; private bool startFading = false; private float timer = 0f; private bool fadeComplete = false; public GameObject startMenuUI; // Assign this in Inspector if needed void Start() { fadeImage = GetComponent(); if (fadeImage == null) { Debug.LogError("FadeToBlack: No Image component found!"); return; } Color startColor = fadeImage.color; startColor.a = 0f; fadeImage.color = startColor; Debug.Log("FadeToBlack: Initialized with alpha 0."); } void Update() { if (startFading && !fadeComplete) { timer += Time.unscaledDeltaTime; float alpha = Mathf.Clamp01(timer / fadeDuration); Color newColor = fadeImage.color; newColor.a = alpha; fadeImage.color = newColor; if (alpha >= 1f) { fadeComplete = true; Debug.Log("FadeToBlack: Fade complete."); OnFadeComplete(); } } } public void StartFade() { Debug.Log("FadeToBlack: StartFade() called."); startFading = true; if (fadeImage != null) { fadeImage.color = new Color(0f, 0f, 0f, 0f); // Ensure it's black and invisible at the start } if (startMenuUI != null && startMenuUI.activeInHierarchy) { Debug.Log("Start menu was still active — disabling it."); startMenuUI.SetActive(false); } } private void OnFadeComplete() { Debug.Log("Fade complete — now locking cursor and pausing game."); Time.timeScale = 0f; Cursor.lockState = CursorLockMode.None; Cursor.visible = true; } }