WaitUntil and WaitWhile don't work

For some reason in my script, the WaitWhile and WaitUntil does not work properly. I am trying to wait until the animation is “DropBuilding” and then wait while it is not finished. Sometimes, the code just stops there when the animation has already finished playing and nothing happens. Here is the code:

Debug.Log("Animation starting to play.");

            // Waits until the current animation is DropBuilding.
            yield return new WaitUntil(() => previewAnimator.GetCurrentAnimatorStateInfo(0).IsName("DropBuilding"));

            // Waits till animation has finished playing.
            yield return new WaitWhile(() => previewAnimator.GetCurrentAnimatorStateInfo(0).normalizedTime >= 1f);

            Debug.Log("Animation has finished.");

You probably destroyed or deactivated the gameobject the coroutine runs on. This will naturally stop / terminate all coroutines that run on that gameobject.

This is all we can guess here as your question is lacking context.