Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Trouble with delegate after scene RELOADS

Discussion in 'Scripting' started by Boo-Let, Jun 12, 2021.

  1. Boo-Let

    Boo-Let

    Joined:
    Jan 21, 2019
    Posts:
    150
    Please note, that either of these codes WORK on the first time scene is loaded. So if he succeeds it's run code one, or if fails, it's run code two.

    once the scene is exited and reloaded, neither of them work. I'm hoping one of you can spot the error and guide me into solving the issue. All of the scripts leading to these, continue to work perfectly.

    Thanks.

    Code one, used when player succeeds.
    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class FinalFadeCut : MonoBehaviour
    7. {
    8.     [Header("Fade Panel")]
    9.     [SerializeField] CanvasGroup panelCanvasGroup;
    10.  
    11.     [Header("Fade Text")]
    12.     [SerializeField] CanvasGroup textCanvasGroup;
    13.  
    14.     [Header("Click to continue")]
    15.     [SerializeField] CanvasGroup clickContinue;
    16.     [SerializeField] bool isDisplayed;
    17.  
    18.     [Header("Properties")]
    19.     [SerializeField] float fadeStep;
    20.  
    21.     private void Awake()
    22.     {
    23.         panelCanvasGroup = transform.GetChild(0).GetComponent<CanvasGroup>();
    24.         {
    25.             panelCanvasGroup.alpha = 0f;
    26.         }
    27.  
    28.         textCanvasGroup = transform.GetChild(1).GetComponent<CanvasGroup>();
    29.         {
    30.             textCanvasGroup.alpha = 0f;
    31.         }
    32.  
    33.         clickContinue = transform.GetChild(2).GetComponent<CanvasGroup>();
    34.         {
    35.             clickContinue.alpha = 0f;
    36.         }
    37.  
    38.         this.gameObject.SetActive(false);
    39.     }
    40.  
    41.     private void OnEnable()
    42.     {
    43.         FinalCut.finalFade -= StartFinalFade;
    44.     }
    45.  
    46.     private void OnDisable()
    47.     {
    48.         FinalCut.finalFade += StartFinalFade;
    49.     }
    50.  
    51.     private void Update()
    52.     {
    53.         if (!isDisplayed)
    54.             return;
    55.  
    56.         if (Input.GetMouseButtonUp(0))
    57.         {
    58.             GameManager.loadScene?.Invoke(0);
    59.             GameManager.endGame.Invoke();
    60.         }
    61.     }
    62.  
    63.     private void StartFinalFade()
    64.     {
    65.         this.gameObject.SetActive(true);
    66.         {
    67.             StartCoroutine(FadePanel());
    68.         }
    69.     }
    70.  
    71.     private IEnumerator FadePanel()
    72.     {
    73.         while(panelCanvasGroup.alpha < 1f)
    74.         {
    75.             panelCanvasGroup.alpha += Time.deltaTime * (fadeStep * 2);
    76.             yield return null;
    77.         }
    78.  
    79.         StartCoroutine(FadeText());
    80.     }
    81.  
    82.     private IEnumerator FadeText()
    83.     {
    84.         while (textCanvasGroup.alpha < 1f)
    85.         {
    86.            textCanvasGroup.alpha += Time.deltaTime * fadeStep;
    87.             yield return null;
    88.         }
    89.  
    90.         StartCoroutine(FadeCTC());
    91.     }
    92.  
    93.     private IEnumerator FadeCTC()
    94.     {
    95.         while (clickContinue.alpha < 1f)
    96.         {
    97.             clickContinue.alpha += Time.deltaTime * fadeStep;
    98.             yield return null;
    99.         }
    100.  
    101.         isDisplayed = true;
    102.     }
    103. }
    104.  
    Code two, used when player fails.
    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class FailScreen : MonoBehaviour
    7. {
    8.     [Header("Panel Properties")]
    9.     [SerializeField] CanvasGroup canvasGroup;
    10.     [SerializeField] float fadeStep;
    11.  
    12.     [Header("Has Displayed")]
    13.     [SerializeField] bool isDisplayed;
    14.  
    15.     private void Start()
    16.     {
    17.         canvasGroup = transform.GetChild(0).GetComponent<CanvasGroup>();
    18.         {
    19.             canvasGroup.alpha = 0f;
    20.         }
    21.  
    22.         transform.gameObject.SetActive(false);
    23.     }
    24.  
    25.     private void OnEnable()
    26.     {
    27.         HealthController.playerDeath -= EnableScreen;
    28.     }
    29.  
    30.     private void OnDisable()
    31.     {
    32.         HealthController.playerDeath += EnableScreen;
    33.     }
    34.  
    35.     private void EnableScreen()
    36.     {
    37.         gameObject.SetActive(true);
    38.         {
    39.             GameManager.roundEnd?.Invoke();
    40.             StartCoroutine(ScreenFade());
    41.         }
    42.     }
    43.  
    44.     private void Update()
    45.     {
    46.         if (!isDisplayed)
    47.             return;
    48.  
    49.         if(Input.GetMouseButtonUp(0))
    50.         {
    51.             GameManager.loadScene?.Invoke(0);
    52.             GameManager.endGame?.Invoke();
    53.         }
    54.     }
    55.  
    56.     private IEnumerator ScreenFade()
    57.     {
    58.         while (canvasGroup.alpha < .9f)
    59.         {
    60.             canvasGroup.alpha += Time.deltaTime * fadeStep;
    61.             yield return null;
    62.         }
    63.  
    64.         isDisplayed = true;
    65.     }
    66. }
    67.  
    68.  
     
  2. Boo-Let

    Boo-Let

    Joined:
    Jan 21, 2019
    Posts:
    150
    Update: So I ran through the game in editor and upon returning to the scene and "Failing" it is throwing an error stating that the gameobject "FailScreen" has been destroyed...even though it is still in the scene.

    So now I know where the problem is..but....it's not being destroyed...
     
  3. Boo-Let

    Boo-Let

    Joined:
    Jan 21, 2019
    Posts:
    150
    Okay so I found a solution but seems kind of dirty.

    I added an OnDestroy() method that unsubscribes to the delegate. Now when replaying the scene everything works just fine.

    Any better solution to this?