Starting a legacy animation with delay from script in Unity

Applies to: Unity 4.5.5f1 (and possibly other versions)

Unity has introduced a new animation system called Mecanim, but the existing animation system is still available to use. If you are using the old animation system you’ll be using an ‘animation’ component rather than an ‘animator’ component on your gameobject. And when you do so, you’ll likely see at least one of these warnings:

The AnimationClip ‘x’ used by the Animation component ‘y’ must be marked as Legacy.
Default clip could not be found in attached animations list.

There are two ways to solve this, depending on whether you have a model (e.g. via a prefab) or not.

You have a model
Select the model and on the Rig tab of the import settings, change the animation type to ‘legacy’.

rig-tab

You don’t have a model
In this case, select the animation and then in the inspector, RIGHT-click the ‘Inspector’ tab and choose ‘Debug’.

inspector-debug

Then change the animation type value from 2 to 1. Then change back to ‘Normal’ mode on the Inspector tab and when you run your app, this warning should be gone.

It is simple to apply your legacy animation to a gameobject by simply adding an ‘animation’ compoment and dragging the animation to appply onto that component. In this case, the animation will by default play on startup of your app. But what if you want to start the animation after some delay via a script, for example. To do this, uncheck ‘Play automatically’ in the animation component, then add a new script component to your game object. You can use the following code to start the animation after a 3 second delay. This also demonstrates how to achieve a fixed delay from a script.

public class startanim : MonoBehaviour {

	private bool started = false;

	// Use this for initialization
	void Start () 
	{
	}
	
	// Update is called once per frame
	void Update ()
	{
		if (!started)
		{
			executeWait();
			started = true;
		}
	}

	private void executeWait()
	{
		StartCoroutine(Wait(3.0f));
		Debug.Log("This line runs *immediately* after starting the Coroutine");
	}
	
	private IEnumerator Wait(float seconds)
	{
		yield return new WaitForSeconds(seconds);
		Debug.Log("3 seconds has passed, animation is now starting.");
		this.animation.Play ();
	}
}

Finally, the debug output can be viewed at the bottom of the unity window:

debug-output