Better to use in-line variables to avoid alpha increasing too quickly

I initially made a mistake that was causing alpha to increase much too rapidly. It took me a moment to figure it out. I thought I’d pass it on: make sure you increment within the while loop, rather than doing what i did below. Calculating a separate variable outside the while loop means you increment by the wrong fixed amount.



        IEnumerator FadeOut(float time)
        {
            **float deltaAlpha = Time.deltaTime / time;**
            while (faderCanvas.alpha < 1)
            {
                faderCanvas.alpha += deltaAlpha;
                yield return null;
            } 
        }
    }

Quite right. If you’re lucky and the framerate holds steady, this could actually work, but it’s best to calculate it within the loop.

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

Privacy & Terms