Making one coroutine wait for another.

I’m having getting one coroutine wait for another to finished because completing the rest of its operation. I’ve already tried using “yield return StartCoroutine(Couroutine())”, but for some reason, I can get the co-routine that is being waited on to finish and allowing the first to proceed.

I’m trying to do the following:

  1. Load 2 files (asset bundles) asynchronously.
  2. Change the scene asynchronously when the asset bundles are loaded.

Here is the code:

//Loading of Asset bundles.
public static IEnumerator LoadBundlesAsynch(string _bundleImage, string _bundleModel) //loads the bundle Asynchronously.
    {
        var imageBundleRequest = AssetBundle.LoadFromFileAsync(Application.persistentDataPath + "/" + _bundleImage);

        yield return imageBundleRequest;      
      
        var modelBundleRequest = AssetBundle.LoadFromFileAsync(Application.persistentDataPath + "/" + _bundleModel);

        yield return modelBundleRequest;

        AssetBundle bundleImage = imageBundleRequest.assetBundle;
        AssetBundle bundleModel = modelBundleRequest.assetBundle;

        if(bundleImage == null || bundleModel == null)
        {
            if(bundleImage == null)
            {
               Debug.Log(Application.persistentDataPath + "/" + _bundleImage + " doesn't exist. Aborting load.");              
            }
            else
            {
                Debug.Log(Application.persistentDataPath + "/" + _bundleModel + " doesn't exist. Aborting load.");              
            }
            yield break;
        }
        else
        {
            Debug.Log(Application.persistentDataPath + "/" + _bundleImage + " loaded successfully.");
            GameManager.currentCartModel = bundleImage;
            Debug.Log(Application.persistentDataPath + "/" + _bundleModel + " loaded successfully.");
            GameManager.currentCartModel = bundleModel;

            yield break;
        }
    }

//scene changer
public IEnumerator SceneChangeAsync(string _sceneName, string _imageBundle, string _modelBundle, bool _wheel = false) //changes scene asynchronously. Has spinning wheel and/or progress bar..
    {
        sceneChangePanel.SetActive(true);
        sceneChangeWheel.SetActive(_wheel);
        progress.gameObject.SetActive(!_wheel);

        if (_wheel)
        {
            spin = true;
            StartCoroutine(WheelSpin());
        }

        Debug.Log("Starting bundle load.");

        yield return StartCoroutine(ResourceLoaderUnloader.LoadBundlesAsynch(_imageBundle, _modelBundle));

        Debug.Log("Bundles loaded. Starting Scene change.");

        AsyncOperation asyncSceneChange = SceneManager.LoadSceneAsync(_sceneName);      

        while (!asyncSceneChange.isDone)
        {
            if(!_wheel)
            {                
                progress.value = asyncSceneChange.progress;              
                yield return new WaitForEndOfFrame();
            }
            else
            {
                yield return new WaitForEndOfFrame();
            }
        }

        spin = false;
        Debug.Log("Load Complete.");
        Destroy(gameObject); //gameobject has DontDestroyOnLoad and destroys itself when done loading.

I’m unsure what I’m doing wrong. I get to the end of the first coroutine and the asset bundles are loaded (debug messages), but then it just stops there. I get the message “Starting bundle load”, followed by the 2 asset bundle loaded Debug Messages. Then it stops and doesn’t go pass that point to the Debug.Log(“Bundles loaded. Starting Scene change.”);

Unsure what I’m missing.

Help much appreciated. :slight_smile:

The reference way to do stuff like this is to make the thing waiting NOT care about what it is waiting for directly.

Every time you fire off a request, increment an integer (let’s call that WorksInProgress)

Every time the completed request comes in, decrement the integer

Over in the thing waiting, wait as long as that number is above zero, then proceed.

Left undefined is how you want to handle errors, such as you got one item but one item returned an error. That’s just a design issue.

EDIT: Problem solved.