Fader flashes for a split second upon starting the game

Hi for some reason even after changing the duration the fader flashes for a second when going itno play mode.

I think it maybe being cancelled out or something upon the second initial load, unless that only happens when going into the portal.

Anyway here is my code:
In saving wrapper:

     private IEnumerator LoadLastScene()
        {
            yield return _savingSystem.LoadLastScene(defaultSaveFile);
            Fader fader = FindObjectOfType<Fader>();
            fader.FadeOutImmediately();
            yield return fader.FadeIn(_fadeInTime);
        }

I’ve tried changing the fade in time, in both the script and the inspector, but it doesn’t make a difference.

This is my relevant code in the fader:

      public IEnumerator FadeIn(float time)
        {
            float fadeInValue = 0;
            return Fade(fadeInValue, time);
        }
       
        public IEnumerator Fade(float target, float time)
        {
            if (_currentActiveFade != null)
            {
                StopCoroutine(_currentActiveFade);
            }
            _currentActiveFade = StartCoroutine(FadeRoutine(target, time));
            yield return _currentActiveFade;
        }
        IEnumerator FadeRoutine(float target, float time)
        {
            while (Mathf.Approximately(_canvasGroup.alpha, target) != true)
            {
                _canvasGroup.alpha = Mathf.MoveTowards(_canvasGroup.alpha, target, Time.deltaTime / time);
                yield return null;
            }
        }
    }

Edit: I just completed the next lecture which changes the code to:

     public Coroutine FadeIn(float time)
        {
            float fadeInValue = 0;
            return Fade(fadeInValue, time);
        }
       
        public Coroutine Fade(float target, float time)
        {
            if (_currentActiveFade != null)
            {
                StopCoroutine(_currentActiveFade);
            }
            _currentActiveFade = StartCoroutine(FadeRoutine(target, time));
            return _currentActiveFade;
        }
        IEnumerator FadeRoutine(float target, float time)
        {
            while (Mathf.Approximately(_canvasGroup.alpha, target) != true)
            {
                _canvasGroup.alpha = Mathf.MoveTowards(_canvasGroup.alpha, target, Time.deltaTime / time);
                yield return null;
            }
        }
    }

Which fixed the issue, how has changing the return types to coroutines fixed this?

It allows us to return, rather than yield return, which prevents a race condition. It’s mainly to avoid the fader gettting stuck. That it solved your issue is what we call frosting on the cake.

1 Like

I’m sorry but I still don’t fully get it, what’s the main difference between just returning and yield returning it.

I’ve in the past looked at the syntatic sugar of yield but I struggle to fully grasp it, is it something to do with that it will iterate through the method if its yield returned?

It’s a difference of purpose. The result of

return _currentActiveFade;

can be stored in a variable, giving us a hook to stop that specific coroutine if we want to.

yield return _currentActiveFade;

will yield control of the method until the _currentActiveFade coroutine is finished, and then go back to that method (which will of course, end). You cannot store the results of a yield return with an IEnumerator. It’s just not set up that way.

1 Like

Privacy & Terms