Coroutine stops after one update. Why?

Hey all,

I am pretty new to coroutines and I have written one, but unfortunately it is carried out in just one update, so it doesn’t finish. I watched some videos about coroutines and found, that with “yield return null” the coroutine should keep running several updates until the condition in the while loop is met. But unfortunately this doesn’t work. After the first update the coroutine is not entered anymore.

That is my pseudo code (else it might be too long):

using System.Collections;
using UnityEngine;

public class CrouchDown
{
    public IEnumerator CrouchingDown()
    {
        /// While the "MoveTowards" is not done, carry out coroutine

        while(rootDistance > 0) 
        {
            Move the root down
            yield return null;
        }

        /// If movement is done, perform 2 other actions and exit the coroutine
        if (rootDistance == 0)
        {
            Adjust character controller collider
            Adjust capsule collider

            yield break;
        }
    }
}

Can somebody help me with this?
Thank you
Huxi

Check that you actually call StartCoroutine to start the coroutine. There are a number of other possible problems that continue from the link.

If it’s none of those, then it’s most likely a logic issue and rootDistance quickly becomes 0 or below. You can the debugger or logging to determine what’s happening there.

Hello ThomIngram,

you’re right, I had a logic mistake there in an IF loop. I wasn’t sure first, if I had used the correct yields.

Thank you for help.
Huxi