Coroutine [sometimes] stops working
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 !
Answer by JonnyRave · Nov 09, 2018 at 08:03 AM
I'm sorry my answer won't be useful but I have the same issue but with fading out audio. although i did read somewhere that the coroutine could stop if the gameobject that called it got destroyed while it was running but not entirely sure.