using System.Collections; using UnityEngine; [RequireComponent(typeof(CanvasGroup))] public class MenuFadeIn : MonoBehaviour { public float delay = 0.3f; public float duration = 3.5f; CanvasGroup _group; Coroutine _fadeRoutine; void Awake() { // Ensure a CanvasGroup exists (SetupMainMenuScene adds one, but be defensive) _group = GetComponent(); if (_group == null) _group = gameObject.AddComponent(); _group.alpha = 1f; _group.blocksRaycasts = true; Debug.Log("[MenuFadeIn] Awake - CanvasGroup ensured, alpha=1"); } void OnEnable() { // Reset state whenever enabled and start the fade if (_group == null) _group = GetComponent() ?? gameObject.AddComponent(); _group.alpha = 1f; _group.blocksRaycasts = true; if (_fadeRoutine != null) StopCoroutine(_fadeRoutine); _fadeRoutine = StartCoroutine(Fade()); Debug.Log("[MenuFadeIn] OnEnable - starting fade (delay=" + delay + ", duration=" + duration + ")"); } void OnDisable() { if (_fadeRoutine != null) { StopCoroutine(_fadeRoutine); _fadeRoutine = null; Debug.Log("[MenuFadeIn] OnDisable - stopped fade coroutine"); } } IEnumerator Fade() { if (delay > 0f) yield return new WaitForSecondsRealtime(delay); float t = 0f; while (t < duration) { t += Time.unscaledDeltaTime; _group.alpha = 1f - Mathf.Clamp01(t / duration); yield return null; } _group.alpha = 0f; _group.blocksRaycasts = false; Debug.Log("[MenuFadeIn] Fade finished - disabling GameObject"); gameObject.SetActive(false); } }