Animations not playing all the time. BUG?

I’ve been experiencing some animations on game objects not playing until the end or even not playing at all in some cases. Most are at random, like sometimes they play and sometimes they don’t, with no changes to the code or assets.

The code for playing most of them is sometimes really straightforward, just a simple Animation.Play(“bla”);

Also, some of them happen to freeze mid animation, just after loading the scene.

This happens in the Unity iPhone 1.5, both in the editor and on the iPhone.

Has anyone experienced this sort of behavior?

Is this happening when a script is used to control playback? If so, can you post the offending script?

Here you go:

IEnumerator StartMenu()
    {
        yield return StartCoroutine(ShowMainMenuButtons());
        while (true)
        {
            yield return StartCoroutine(MenuCheck());
        }
    }
    IEnumerator ShowMainMenuButtons()
    {
        buttonResume.SetActiveRecursively(false);
        buttonOptions.SetActiveRecursively(false);
        buttonOnline.SetActiveRecursively(false);
        buttonNewGame.animation.Play("appear");
        yield return new WaitForSeconds(buttonAppearTime);
        buttonResume.SetActiveRecursively(true);
        buttonResume.animation.Play("appear");
        yield return new WaitForSeconds(buttonAppearTime);
        buttonOptions.SetActiveRecursively(true);
        buttonOptions.animation.Play("appear");
        yield return new WaitForSeconds(buttonAppearTime);
        buttonOnline.SetActiveRecursively(true);
        buttonOnline.animation.Play("appear");
        yield return 0; 
    }

The ‘MenuCheck’ coroutine checks for touch inputs.
I’m not really sure what ‘yield return 0’ does; besides waiting for the frame to finish, does it behave like a regular return out of a function ?

So in this example, sometimes the animations play to the end just fine, and sometimes it freezes in the middle of the animation, and stays like that.

I’ve had a problem like this with a door opening and closing animation. What happens is that the animation plays, but doesn’t always play the final frame(s), so the door was often left ajar. I think this might have to do with low framerate situations.

The solution I came up with was to add two one frame animations called “opened” and “closed” that are played after the “opening” and “closing” animation to guarantee the door is in the correct position.

Also, there’s another way to yield a coroutine while an animation is playing:

buttonResume.animation.Play("appear"); 
while (buttonResume.animation.isPlaying)
     yield return 0; 
buttonResume.animation.Play("visibleposition");

This way you don’t have to store the animation’s length somewhere.