Search Unity

Coroutine [somtimes] stopps working

Discussion in 'Scripting' started by genaray, Jul 20, 2017.

  1. genaray

    genaray

    Joined:
    Feb 8, 2017
    Posts:
    191
    Hey there !

    Im currently trying to make a smooth fadeout of a canvas group. So i have this class here as an helper :


    Code (CSharp):
    1. public class UIgroup {
    2.  
    3.     public static float time = 1f;
    4.  
    5.  
    6.     public static IEnumerator canvasFadeIn(CanvasGroup canvasGroup)
    7.     {
    8.  
    9.         while (canvasGroup.alpha <= 1)
    10.         {
    11.             canvasGroup.alpha += Time.deltaTime / time;
    12.             yield return null;
    13.  
    14.  
    15.         }
    16.  
    17.  
    18.     }
    19.  
    20.  
    21.     public static IEnumerator canvasFadeOut(CanvasGroup canvasGroup)
    22.     {
    23.         //if(canvasGroup.alpha >= 0)
    24.  
    25.         while (canvasGroup.alpha >= 0)
    26.         {
    27.             canvasGroup.alpha -= Time.deltaTime / time;
    28.             yield return null;
    29.  
    30.  
    31.         }
    32.  
    33.  
    34.     }
    35.  
    36.  
    37.     public static void resetScene() {
    38.  
    39.         SceneManager.LoadScene("Illud");
    40.  
    41.     }
    42.  
    43.  
    44.  
    45.     }
    46.  
    47.  
    48.     public class StartTextButton : MonoBehaviour, ButtonInterface {
    49.  
    50.  
    51.     public CanvasGroup startScreenCanvasGroup;
    52.  
    53.     public CanvasGroup gameScreenCanvasGroup;
    54.  
    55.  
    56.     public void onTouchUp()
    57.     {
    58.  
    59.         StopAllCoroutines();
    60.  
    61.         StartCoroutine(UIgroup.canvasFadeOut(startScreenCanvasGroup));
    62.  
    63.         StartCoroutine(UIgroup.canvasFadeIn(gameScreenCanvasGroup));
    64.  
    65.         Camera.main.GetComponent<Main>().startTextWasClicked = true;
    66.  
    67.         Debug.Log("Clicked");
    68.  
    69.  
    70.  
    71.      }
    72.  
    73.     }
    When i click for example on the start button, i call fadeOut like above in the OnClick method.

    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 !
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    Are you getting any exceptions?
     
  3. makeshiftwings

    makeshiftwings

    Joined:
    May 28, 2011
    Posts:
    3,350
    Coroutines are attached to whichever component calls them, and if you delete or disable that component, they stop. In your case, they're attached to the StartTextButton button, so I'd guess something else is deleting or disabling that button during the screen fade.
     
    Kurt-Dekker likes this.
  4. genaray

    genaray

    Joined:
    Feb 8, 2017
    Posts:
    191
    @Kurt-Dekker Nope, there are no exceptions :/
    @makeshiftwings the Problem is, that i dont disable/delete the button. I just set the canvasGroup.interactable = false; later. But thats all, the Button stays active all the time ...
     
  5. genaray

    genaray

    Joined:
    Feb 8, 2017
    Posts:
    191
  6. genaray

    genaray

    Joined:
    Feb 8, 2017
    Posts:
    191
  7. BlackPete

    BlackPete

    Joined:
    Nov 16, 2016
    Posts:
    970
    Is onTouchUp getting called extra times? Do you get extra "Clicked" log lines? (i.e. check the log)
     
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    Try disable the load scene entirely and see if the fading finishes fully, so you get some intel about what might be interrupting it
     
  9. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    how are you ensuring that the scene load is not called until the coroutines are done? I don't see that in the above codelets.
     
  10. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    I might suggest switching to a tweening engine as you can easily set up the fade and add calls to happen when the tween finishes, especially if you are having trouble rolling your own fade system.
     
    Kurt-Dekker likes this.