I'm trying to fade a background image in and out. At some point I will fade new images in and fade old one out. Right now just trying to fade a single image in and out. Not working: code: Code (CSharp): using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class Fade_Background : MonoBehaviour { public Image BackgroundImage; private bool _FadeIn = false; private bool _FadeOut = false; private void Start() { _FadeOut = true; } void Update() { if(_FadeOut) { StartCoroutine(FadeOut()); } else { StartCoroutine(FadeIn()); } } IEnumerator FadeOut() { BackgroundImage = GetComponent<Image>(); var tmpColor = BackgroundImage.color; while(BackgroundImage.color.a>0) { tmpColor.a -= Time.deltaTime / 2; BackgroundImage.color = tmpColor; yield return null; } _FadeOut = false; _FadeIn = true; yield return null; } IEnumerator FadeIn() { BackgroundImage = GetComponent<Image>(); var tmpColor = BackgroundImage.color; while (BackgroundImage.color.a < 0) { tmpColor.a += Time.deltaTime / 2; BackgroundImage.color = tmpColor; yield return null; } _FadeOut = true; _FadeIn = false; yield return null; } }
Right line, wrong fix. Should be < 1f, not < 255. Colors are 0 through 1. That's not the only issue though. The main issue is that you're starting a new coroutine every frame. In Update(), you're constantly either starting FadeOut or starting FadeIn. These coroutines just sort of stack up every frame, each one constantly overwriting BackgroundImage.color. Easiest fix here is to not use an "else" in Update (check for each bool individually), and at the start of both coroutines, set both bools to false - only set one of them true when you're ready for Update to call the next coroutine.
I got it working, here is the code: Code (CSharp): using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class Fade_Background : MonoBehaviour { public Image BackgroundImage; public int _Delay; private bool _FadeOut = false; private bool _Running = false; private void Start() { GameObject imageObject = GameObject.FindGameObjectWithTag("imgBackground"); BackgroundImage = imageObject.GetComponent<Image>(); } void Update() { if (!_Running) { if (_FadeOut) { StartCoroutine(FadeOut()); } else { StartCoroutine(FadeIn()); } } } IEnumerator FadeOut() { _Running = true; if (BackgroundImage == null) { GameObject imageObject = GameObject.FindGameObjectWithTag("imgBackground"); BackgroundImage = imageObject.GetComponent<Image>(); } Color tmpColor = BackgroundImage.color; while (BackgroundImage.color.a > 0.0f) { tmpColor.a -= Time.deltaTime / 4; BackgroundImage.color = tmpColor; yield return null; } _FadeOut = false; _Running = false; yield return null; } IEnumerator FadeIn() { _Running = true; if (BackgroundImage == null) { GameObject imageObject = GameObject.FindGameObjectWithTag("imgBackground"); BackgroundImage = imageObject.GetComponent<Image>(); } Color tmpColor = BackgroundImage.color; while (BackgroundImage.color.a < 1.0f) { tmpColor.a += Time.deltaTime / 4; BackgroundImage.color = tmpColor; yield return null; } yield return new WaitForSecondsRealtime(_Delay); _FadeOut = true; _Running = false; yield return null; } }