Search Unity

Getting multiple Coroutines to run in tandem

Discussion in 'Scripting' started by chuakc92, May 3, 2018.

  1. chuakc92

    chuakc92

    Joined:
    Nov 22, 2016
    Posts:
    20
    I understand that if you StartCoroutine(), it runs an instance of it.

    I'm trying to do the following:
    Code (CSharp):
    1. .......
    2.             for (int i = 0; i < _crate.slots.Length;i++)
    3.                 StartCoroutine(CastAndShow(_crate.slots[i], i));
    4. .....
    5.  
    6.  
    7. private IEnumerator CastAndShow(CrateSlot _slot, int _index)
    8.         {
    9.         ........          
    10.                 yield return new WaitUntil(() => userInput);
    11. .        .....
    12.     }
    So all instances of it will have to wait for a userInput bool to turn true, then they should all continue.

    This doesn't work, however. Only one instance of it continues each time. How do I get all of them to continue?
     
  2. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    You're probably resetting your userInput variable as a part of one of CastAndShow. Could you post more code?

    For reference, this prints all the tests on one keypress:

    Code (csharp):
    1. public class TestScript : MonoBehaviour {
    2.  
    3.     void Start() {
    4.         for (int i = 0; i < 5; i++) {
    5.             StartCoroutine(Test(i));
    6.         }
    7.     }
    8.  
    9.     private IEnumerator Test(int i) {
    10.         yield return new WaitUntil(() => Input.GetKeyDown(KeyCode.Space));
    11.  
    12.         Debug.Log("test " + i);
    13.     }
    14. }
     
  3. chuakc92

    chuakc92

    Joined:
    Nov 22, 2016
    Posts:
    20
    Hello Baste, thank you for responding.

    I am actually resetting userInput at the end of the Coroutine "CastAndShow". Thank you so much for pointing that out!

    I'll try removing that tomorrow and see if it works. Thanks again!

    Edit: I can confirm now that it was my resetting userInput at the bottom of the Coroutine that caused the stoppage.
     
    Last edited: May 4, 2018