Fader gets stuck when starting the game

After following this lecture, my fader is stuck as soon as i play my game, resulting in a blank white screen.

My Fader.cs file looks like this:

using System.Collections;
using UnityEngine;

namespace GoL.SceneManagement
{
    public class Fader : MonoBehaviour
    {
        CanvasGroup _canvasGroup;
        Coroutine _currentActiveFade = null;

        private void Awake()
        {
            _canvasGroup = GetComponent<CanvasGroup>();
        }

        public void FadeOutImmediate()
        {
            _canvasGroup.alpha = 1;
        }

        public IEnumerator FadeOut(float time)
        {
            return Fade(1, time);
        }

        public IEnumerator FadeIn(float time)
        {
            return Fade(0, time);
        }

        public IEnumerator Fade (float target, float time)
        {
            if (_currentActiveFade != null)
            {
                StopCoroutine(_currentActiveFade);
            }
            _currentActiveFade = StartCoroutine(FadeRoutine(target, time));
            yield return _currentActiveFade;
        }

        private IEnumerator FadeRoutine(float target, float time)
        {
            while (Mathf.Approximately(_canvasGroup.alpha, target))
            {
                _canvasGroup.alpha = Mathf.MoveTowards(_canvasGroup.alpha, target, Time.deltaTime / time);
                yield return null;
            }
        }
    }
}

I’m using Unity version 2020.3.20f1.

Thanks in advance!

1 Like

Are any error messages appearing in the console?

Without an outside class calling Fader, the script won’t do anything. Paste in your SavingWrapper.cs script, which is the first thing that calls the Fader.

No, no error messages either.

Here’s my SavingWrapper.cs code.

using GoL.Saving;
using System.Collections;
using UnityEngine;

namespace GoL.SceneManagement
{
    public class SavingWrapper : MonoBehaviour
    {
        const string _defaultSaveFile = "save";
        [SerializeField] float _fadeInTime = 0.2f;

        private void Awake()
        {
            StartCoroutine(LoadLastScene());
        }

        IEnumerator LoadLastScene()
        {
            yield return GetComponent<SavingSystem>().LoadLastScene(_defaultSaveFile);
            Fader _fader = FindObjectOfType<Fader>();
            _fader.FadeOutImmediate();
            yield return _fader.FadeIn(_fadeInTime);
        }

        private void Update()
        {
            if (Input.GetKeyDown(KeyCode.L))
            {
                Load();
            }

            if (Input.GetKeyDown(KeyCode.S))
            {
                Save();
            }

            if (Input.GetKeyDown(KeyCode.Delete))
            {
                Delete();
                print("Save file deleted");
            }
        }

        public void Save()
        {
            GetComponent<SavingSystem>().Save(_defaultSaveFile);
        }

        public void Load()
        {
            GetComponent<SavingSystem>().Load(_defaultSaveFile);
        }

        public void Delete()
        {
            GetComponent<SavingSystem>().Delete(_defaultSaveFile);
        }
    }
}

Thanks for helping me out again.

However, when I set the _canvasGroup.alpha to 0 in the FadeOutImmediate() method in my Fader.cs, the game plays as intended, except there’s no faders at all. Don’t know if that helps at all.

Try adding this to your Fader.FadeRoutine() in the While loop between the alpha setting and the yield return null

Debug.Log($"Fader CanvasGroup = {_canvasGroup.alpha}");

That should spam the console with a lot of floats, going from 0 to 1 when the scene starts.

The plot thickens… I put in the line you asked me like this:

private IEnumerator FadeRoutine(float target, float time)
        {
            while (Mathf.Approximately(_canvasGroup.alpha, target))
            {
                _canvasGroup.alpha = Mathf.MoveTowards(_canvasGroup.alpha, target, Time.deltaTime / time);
                Debug.Log($"Fader CanvasGroup = {_canvasGroup.alpha}");
                yield return null;
            }
        }

When my game and Fader starts it just shows the blank white screen as before with no console messages. But when I (blindly) move to a portal i get the follwoing message:

“Fader CanvasGroup = 1”

Which makes sense. So it looks like the Fader isn’t changing like it’s supposed to.

Is that message just once, or is it stacking to 999+?

I want to ammend the Debug, as well… make it

Debug.Log($"Fader CanvasGroup = {_canvasGroup.alpha}, target = {target}");

It spams the colsole log, but still only when i move towards a portal. The message with your new code is:

“Fader CanvasGroup = 1, target = 1
UnityEngine.Debug:Log (object)”

Odd… since the only method chain that should be reaching that Coroutine is the call to FadeIn

Next Debug:

Debug.Log($"alpha = {_canvasGroup.alpha}, target = {target}, time={time}, interval = {Time.deltaTime/time}");

Still no message when starting the game, but I get this now when entering a portal:

“alpha = 1, target = 1, time=0.5, interval = 0.0125594
UnityEngine.Debug:Log (object)”

The interval changes with each message.

I just popped back into the course code, and I spotted a key difference…
You have

while (Mathf.Approximately(_canvasGroup.alpha, target))

but the course has

while (!Mathf.Approximately(canvasGroup.alpha, target))

I’m not concerned with the underscore, that’s a preference issue, the problem is with the !.. or lack thereof…
I’m pretty sure what’s happening is that FadeIn never gets a chance to run,because a Fade from 1 to 0 will fail the While check immediately, and never adjust the canvas (or debug!) But when you Fadeout, with that Canvas at 1, then the While loop will never end, locking you up at 1.
Slip that ! in and see what happens, you should start seeing debugs when the game loads, then debugs to fade out, and debugs to fade in when you go through the next portal.

3 Likes

That was it!!

I guess I wasn’t observant enough when I followed along or reading the GitHub commit. Really nice catch.

Thank you so much for helping me out again!

Don’t feel too bad, that was my 5th reading of the code, and I missed it. I did have the clue that FadeIn didn’t appear to be getting called (FadeIn would have a target of 0), and you had no messages till you portalled, which of course starts with a FadeOut… That got me going back to the source code, and then finally I noticed the !
It’s just like my teacher told me in middle school (a long long long long time ago)… “Punctuation matters”

I’m glad this one got sorted.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms