Waiting for a Nested Coroutine to Finish
I'm trying to wait for a nestesd coroutine to end inside another coroutine.
I have the following code:
public IEnumerator Fall()
{
controller.DisableMovement();
animationManager.animator.SetBool("Falling", true);
while (!controller.IsGrounded())
{
yield return null;
}
animationManager.animator.SetBool("Falling", false);
print(1);
if (controller.HardLanding())
{
yield return StartCoroutine(HardLand());
}
else
{
yield return StartCoroutine(Land());
}
print(4);
}
public IEnumerator HardLand()
{
print(2);
animationManager.animator.SetTrigger("Hard Land");
yield return new WaitForSeconds(animationManager.GetAnimationClip(animationManager.clipNames.hardLanding).length + 5f);
animationManager.animator.ResetTrigger("Hard Land");
controller.EnableMovement();
print(3);
}
The print order should be 1, 2, 3 & 4. But I'm getting 1, 2, 4, 3, and don't know how to fix this.
Answer by HACFelipe · Apr 08, 2020 at 01:13 PM
Actually I got this fixed.
The problem was in the main code. I was calling the coroutine in Update method, but what I was actually trying to do is call the coroutine only once. Setting a boolean flag solved the problem!
Answer by dvxl · Oct 30, 2020 at 07:54 PM
If someone wonders, there is a way to wait until its finished. Just figured it out, its nowhere else in the internet:
just use yield return StartCoroutine(blabla()); inside the other coroutine instead of just StartCoroutine(blabla()); and there you go!
Your answer
Follow this Question
Related Questions
Unity C# Punching Coroutine not completing 0 Answers
How to properly call Async Task 0 Answers
Wait until event fires in coroutine 0 Answers