• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
19
Question by FLASHDENMARK · Jan 28, 2011 at 07:35 PM · animation

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 ;)

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

8 Replies

· Add your reply
  • Sort: 
avatar image
25
Best Answer

Answer by Bunny83 · Jan 28, 2011 at 07:59 PM

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");
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image FLASHDENMARK · Jan 28, 2011 at 08:10 PM 0
Share

Thank, you very much :)

avatar image
65

Answer by ShawnFeatherly · Feb 10, 2014 at 08:59 AM

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

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Kiichi · Feb 24, 2014 at 01:40 PM 0
Share

It doesn't work if you use AnimatorController.

avatar image Jeste · Apr 28, 2016 at 10:32 AM 0
Share

This solved partily my problem. I had on one side, ( checked loop-time ), but on other ( wrap-mode set to be once ). With this it loops forever, but when i ( uncheck this loop time ), and ( wrap-mode is set to once ), it only once goes. But when i set ( wrap-mode to loop ) and ( unchec loop-time ), it again play only once. How i can change this loop time in script? I ask this because i open similar thread, take a look if u can, maybe it give u more clearification... http://forum.unity3d.com/threads/how-to-change-wrapmode-of-animator.400736/ How can i play once first animation, and then smoothly transit to play second to infinity? I have looked into CrossFade but it just delay?

avatar image Zagaz · Feb 15, 2017 at 12:50 AM 0
Share

That was the best answer! I spend some good hours trying to figure it out how to do it, and your answer was straight to the point!

Thanks for the help!

avatar image
2

Answer by vickygroups · Apr 30, 2014 at 05:41 AM

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
3

Answer by joshmann971 · Jan 28, 2015 at 11:57 PM

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by GameMechanics · Jul 19, 2015 at 06:13 PM

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
  • 1
  • 2
  • ›

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Play animation once 1 Answer

Play animation for horizontal and vertical movement? 1 Answer

Mixamo Call Animation from Script 0 Answers

Adding animation clips via script 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges