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:
- Load 2 files (asset bundles) asynchronously.
- 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. ![]()