Changing enum breaks a coroutine

Hello!

I have a big coroutine with 2 cycles (one in another). In first cycle its check swayState, which is enum. Depends on this enum, different values sets in variables. After this check, the second cycle begins, that do MoveTowards stuff depends on previously set variables. In the end swayState must change, but here is the problem - changing swayState inside coroutine causes a freeze of unity and I should restart program. I checked through debug, problem is definitely in a changing of swayState.

I can fix this, making coroutine without one cycle in another, but it won’t be a shot elegant solution, I think. Also I’m just curious the reason of freeze.

private IEnumerator Swaying()
{
    Vector2 currentSway = Vector2.zero;
    Vector2 targetSway = Vector2.zero;
    Ray2D ray;
    float randomDirectionX = 0;
    float randomDirectionY = 0;
    Vector2 randomDirection;
    float angleChangeX = 0;
    float angleChangeY = 0;

    while (true)
    {
        if (swayingState == SwayingState.RandomMoving)
        {
            randomDirectionX = UnityEngine.Random.Range(-1f, 1f);
            randomDirectionY = UnityEngine.Random.Range(-1f, 1f);

            randomDirection = new Vector2(randomDirectionX, randomDirectionY);
            ray = new Ray2D(Vector2.zero, randomDirection);
            targetSway = ray.GetPoint(swayDistance);
        }
        else if (swayingState == SwayingState.RandomTurning)
        {
            angleChangeX = randomDirectionX * swayAngleChange;
            angleChangeY = randomDirectionY * swayAngleChange;

            randomDirectionX = GetRandomWithinRange(randomDirectionX - angleChangeX, randomDirectionX + angleChangeX);
            randomDirectionY = GetRandomWithinRange(randomDirectionY - angleChangeY, randomDirectionY + angleChangeY);

            randomDirection = new Vector2(randomDirectionX, randomDirectionY);
            ray = new Ray2D(currentSway, randomDirection);
            targetSway = ray.GetPoint(swayDistance / 2);
        }
        else if (swayingState == SwayingState.ReturningToCenter)
        {
            targetSway = Vector2.zero;
        }

        while (currentSway != targetSway)
        {
            Vector2 swayDelta = Vector2.MoveTowards(currentSway, targetSway, swaySpeed) - currentSway;
            MoveSight(swayDelta);
            currentSway += swayDelta;
            yield return new WaitForEndOfFrame();
        }

        //THE PROBLEM!
        if (swayingState == SwayingState.RandomMoving) swayingState = SwayingState.RandomTurning;
        else if (swayingState == SwayingState.RandomTurning) swayingState = SwayingState.ReturningToCenter;
        else if (swayingState == SwayingState.ReturningToCenter) swayingState = SwayingState.RandomMoving;

        yield return new WaitForEndOfFrame();
    }
}

No se mucho de programación así que tu código es difícil de leer. seria bueno crear funciones para mejorar la organización, así seria mas fácil encontrar el problema.
Imagino que estas usando variables estáticas en otros script. Y estas comprando y cambiando los valores al mismo tiempo. Es posible que el programa no tenga tiempo para realizar las dos tareas.

Translation:
I don’t know much about programming so your code is hard to read. It would be good to create functions to improve the organization, so it would be easier to find the problem.
I imagine that you are using static variables in other scripts. And you are buying and changing the values ​​at the same time. The program may not have time to perform both tasks.

The problem occurred when trying to change the state to RandomTurning, as this was leading to the GetRandomWithinRange method, which used a while loop.

The loop was infinite because of one wrong value in SerializeField.