Play animation ONLY once?

Hello Unity pros ;)

Is there a easy way of playing a animation only ONCE? Like som thing like:

animation.PlayOneShot("");

I know you cant do the above example, but it was just an example. I could put something together in a few lines of code so it only would play the animation once, but if there is a easy way of doing so, that would be awesome. ;)

Thanks, you awesome people ;)

If you’ve created an animation using the Animator window, find where you saved the *.anim file using your Project hierarchy window and select it. Then uncheck Loop Time in the inspector.

Loop Time property set to only play the animation once

Just use animation.Play() or animation.CrossFade() as usual but first set the wrapmode of your clip to WrapMode.Once. If you set the wrapmode in the inspector to "once" you don't need the first line.

animation["AnimationName"].wrapMode = WrapMode.Once;
animation.Play("AnimationName");

Hey guys, if the wrap mode “Once” doesn’t work for you, I set it to “Clamp Forever” and it worked for me (just some doors opening once a boolean is true) hope this helps you with your door problems! Don’f forget to go in the debug window on the actual “animationname”.anim file and set “Animation Type” to 1

If you use Animator, you can use Trigger Parameter
and then call your animator like this:

                PlayerAnimator.SetTrigger("tUsingObject");

and animation will play just once.

Shawn’s solution worked for me. The trick is not to select the game object, but rather the file that it made to control the animation. It’s in your asset folder and called whatever file name you called it.

I created a one time and then go away piece of code that may help.

using UnityEngine;
using System.Collections;

public class Explosion : MonoBehaviour
{
	private Animator animator;

	void Start()
	{
		animator = GetComponent<Animator>() as Animator;
	}
	

	void Update()
	{
		AnimatorStateInfo asi = animator.GetCurrentAnimatorStateInfo(0);

		if (!asi.IsName("Explosion") || asi.normalizedTime >= 1)
		{
			Destroy(gameObject);
		}
	}
}

My GameObject has a SpriteRenderer and an Animator. I checked Loop off on the animation. I hope that helps.

In the animator window do not create a transition between the first and second animations. It is enough to create a transition from the second to the first. The second animation is through the script, not through the transition condition.

Try to use in animator parametr trigger (not bool). Same problem https://forum.unity.com/threads/mecanim-automatic-setbool-after-animation-ends.175196/
:frowning: anim.SetBool(“Dance”, true); //It will be play while SetBool is true.

            :)      anim.SetTrigger("Dance");    // It will be play once.