Hey there !
Im currently trying to make a smooth fadeout of a canvas group. So i have this class here as an helper :
public class UIgroup {
public static float time = 1f;
public static IEnumerator canvasFadeIn(CanvasGroup canvasGroup)
{
while (canvasGroup.alpha <= 1)
{
canvasGroup.alpha += Time.deltaTime / time;
yield return null;
}
}
public static IEnumerator canvasFadeOut(CanvasGroup canvasGroup)
{
//if(canvasGroup.alpha >= 0)
while (canvasGroup.alpha >= 0)
{
canvasGroup.alpha -= Time.deltaTime / time;
yield return null;
}
}
public static void resetScene() {
SceneManager.LoadScene("Illud");
}
}
When i click for example on the start button, i call fadeOut like this :
public class StartTextButton : MonoBehaviour, ButtonInterface {
public CanvasGroup startScreenCanvasGroup;
public CanvasGroup gameScreenCanvasGroup;
public void onTouchUp()
{
StopAllCoroutines();
StartCoroutine(UIgroup.canvasFadeOut(startScreenCanvasGroup));
StartCoroutine(UIgroup.canvasFadeIn(gameScreenCanvasGroup));
Camera.main.GetComponent<Main>().startTextWasClicked = true;
Debug.Log("Clicked");
}
}
But sometimes suddenly the coroutine stopps working and the screen stays at about 0.5 alpha, or sometimes even 0.9 alpha. But that only happens sometimes, mostly its working. But when not its annoying and looks very strange. Does anyone have an idea, why this happens ? Thats by the way nearly all of my code. I have some more code that manages the canvas group interactable and block raycasting variables. But thats all.
Thanks for your help !