- Home /
Coroutine stops after any yield statement halfway through
Hi - I have a coroutine that calls several other coroutines in sequence. So far I have been quite successful at using nested coroutines to sequence certain animations, etc., but this particular one has been causing me trouble for a while. The (simplified) coroutine is as follows:
public IEnumerator UnitDeploy(UnitCardView deployed, UnitView target)
{
// Multiple yield returns before this which function fine
UnitView deployView = RowView.DeployedUnit;
RowView.DeployedUnit = null;
Debug.Log($"{deployView} {deployView.MyUnit}");
// prints a non-null MonoBehaviour class name for deployView
if (target == null) {
Debug.Log("Starting playdeploy coroutine"); // prints this
yield return deployView.PlayDeploy();
Debug.Log("Started deploy animation"); // does not print this
deployCard.ActionPrimary(null);
} else {
Debug.Log("Has target - playing secondary cast"); // prints this
deployCard.ActionPrimary(target.MyUnit);
StartCoroutine(deployView.PlayCast(true));
yield return deployView.AnimateSecondary(target, deployCard);
Debug.Log("Done playing deploy secondary animation"); // does not print this
}
}
PlayDeploy() and AnimateSecondary() are both coroutines to which I've added a Debug.Log as the final line - this line also always prints. I don't understand how it's possible that the last line of the called coroutine executes but not the next one in the caller? yield return AnimateSecondary() is called from other coroutines in this file and works fine, so I'm pretty sure the issue is somewhere in this method...
Adding the PlayCast() code as requested... but this, too, is called from other places and works fine from there.
public virtual IEnumerator PlayCast(bool secondary) {
GameObject muzzlePrefab = ... // gets a file from Resources
AudioClip muzzleSound = ... // gets a file from Resources
yield return PlayVFX(muzzlePrefab, muzzleSound); // plays these files - executes correctly
}
debug the values of target and deployCard, also you should post deployView.PlayCast()
Hi - thanks for the feedback - adding the playcast() code but again, it is called from multiple places and works fine.
I am checking target earlier in the method - it is sometimes null and sometimes not, as expected. But in both cases the yield returns stop the coroutine.
I've even tried to set a bool at the end of PlayDeploy() and AnimateSecondary() and change the caller code to e.g. a startcoroutine(PlayDeploy()) followed by a yield return new WaitUntil(() => boolName) but even this does not work, despite the bool getting set... so it seems after a point yield statements simply kill this caller coroutine?