Legacy Documentation: Version 4.6(go to latest)
Language: English
  • C#
  • JS
  • Boo

Script language

Select your preferred scripting language. All code snippets will be displayed in this language.

Animation.Play

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Sumbission failed

For some reason your suggested change could not be submitted. Please try again in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Switch to Manual
public function Play(mode: PlayMode = PlayMode.StopSameLayer): bool;
public bool Play(PlayMode mode = PlayMode.StopSameLayer);
public def Play(mode as PlayMode = PlayMode.StopSameLayer) as bool
public function Play(animation: string, mode: PlayMode = PlayMode.StopSameLayer): bool;
public bool Play(string animation, PlayMode mode = PlayMode.StopSameLayer);
public def Play(animation as string, mode as PlayMode = PlayMode.StopSameLayer) as bool

Description

Plays animation without any blending.

Play() will start animation with name animation, or play the default animation. The animation will be played abruptly without any blending.

If mode is PlayMode.StopSameLayer then all animations in the same layer will be stopped. If mode is PlayMode.StopAll then all animations currently playing will be stopped.

If the animation is already playing, other animations will be stopped but the animation will not rewind to the beginning.

If the animation is not set to be looping it will be stopped and rewinded after playing.

Play() will return false if animation can't be played (no animation clip or no default animation).

	// Plays the default animation
	animation.Play();
using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
    void Example() {
        animation.Play();
    }
}
import UnityEngine
import System.Collections

public class ExampleClass(MonoBehaviour):

	def Example() as void:
		animation.Play()

For a specific animation, you can call the animation with play as well.

	// Plays the walk animation - stops all other animations in the same layer
	animation.Play("walk");
	// Plays the walk animation - stops all other animations
	animation.Play("walk", PlayMode.StopAll);
using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
    void Example() {
        animation.Play("walk");
        animation.Play("walk", PlayMode.StopAll);
    }
}
import UnityEngine
import System.Collections

public class ExampleClass(MonoBehaviour):

	def Example() as void:
		animation.Play('walk')
		animation.Play('walk', PlayMode.StopAll)