Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Nested Coroutines oscillating between two yields?

Discussion in 'Scripting' started by FireFox2000000, Oct 30, 2014.

  1. FireFox2000000

    FireFox2000000

    Joined:
    Oct 28, 2014
    Posts:
    10
    The code I have here is designed to fade to a black screen and then fade back to normal, however as I this gets played it fades to the black screen fine, but when it starts to fade back it starts going a bit spastic and oscillating between the two and never ending.

    The console output tends to go a bit spastic too, with the output following the pattern of "In", "Out", "Out" over and over. I seriously have no idea why it's not returning control to the second function even after looking at many examples online.

    Code (CSharp):
    1. IEnumerator respawn (character player) {
    2.         Debug.Log ("Out");
    3.         yield return StartCoroutine(fade_to_black.fade_out());
    4.         // respawn character
    5.         Debug.Log ("In");
    6.         yield return StartCoroutine(fade_to_black.fade_in());
    7.         player.set_state (character.player_state.active);
    8.  
    9.     }
    Code (CSharp):
    1. public static IEnumerator fade_out () {
    2.         while (opacity < 1) {
    3.             opacity += fade_speed * Time.deltaTime;
    4.             yield return null;
    5.         }
    6.         opacity = 1;
    7.     }
    8.  
    9.     public static IEnumerator fade_in () {
    10.         while (opacity > 0) {
    11.             opacity -= fade_speed * Time.deltaTime;
    12.             yield return null;
    13.         }
    14.         opacity = 0;
    15.     }
     
  2. DoomSamurai

    DoomSamurai

    Joined:
    Oct 10, 2012
    Posts:
    159
    If you see "Out", "Out", "Out", "Out", etc in your console, that would mean the respawn() coroutine has been called multiple times. I'd look into that.