Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • 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
7
Question by Tyyy1997 · Mar 23, 2014 at 04:54 AM · animation2danimatorgraphics

Delete Object After Animation (2D)

Alright, so there's plenty of information on how to do this with 3D animations, but I'm stumped with Unity 4 on how to do this in 2D. I have a very short animation, it's close to a second long, what I want to do is have it play once, and then delete itself. I tried doing:

         if (!animation.isPlaying) {
             Destroy (gameObject);
         }

However, this gives me an error saying there is not animation connected. The only other way is to calculate how long the animation is in total, and then set a timer to delete after it's done, however that gives me little flexibility. What am I supposed to do, how would I achieve this?

Comment
Add comment · Show 3
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 supernat · Mar 23, 2014 at 05:08 AM 0
Share

I think your first approach is fine as long as the animation is playing when you start the scene. But you shouldn't get the error you got if things are setup right. You have a game object with an Animation component on it with at least 1 Animation in the Animations list on that component, in the Inspector. Then you attach the script above to that same game object. It should work. Is this not how you set your scene up? Could you describe, by Animation, do you mean an Animation Component?

avatar image Tyyy1997 · Mar 23, 2014 at 05:12 AM 0
Share

@supernat

alt text

I use this way of doing it, if I try doing the way you say it gives me a warning about the animation having to be in Legacy.

avatar image supernat · Mar 25, 2014 at 01:45 AM 0
Share

Well you said you were using animation, not animator. I was kind of wondering, should have asked you. :)

6 Replies

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

Answer by firestoke · Apr 06, 2016 at 09:20 AM

Here is my solution:
I create a script named "AnimationAutoDestroy", and add this component to the animator game object which you want to auto-destroy while animation is finished. You can adjust the delay time as you want.

 using UnityEngine;
 using System.Collections;
 
 public class AnimationAutoDestroy : MonoBehaviour {
     public float delay = 0f;
 
     // Use this for initialization
     void Start () {
         Destroy (gameObject, this.GetComponent<Animator>().GetCurrentAnimatorStateInfo(0).length + delay); 
     }
 }
Comment
Add comment · Show 2 · 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 JackKokah · Jun 07, 2018 at 07:35 PM 0
Share

great solution

avatar image Eldoir · Feb 19, 2020 at 02:15 PM 0
Share

Please note that this does not take the animation speed into account, and probably other parameters that could be useful in the future. The right way to do this is in the answer by @vElumi

avatar image
38

Answer by vElumi · Jul 22, 2019 at 06:30 PM

You can use StateMachineBehaviour.

  1. In animator, select state. Game object will be destroyed after animation on that state will have finished.
  2. Click Add Behaviour alt text
  3. Override OnStateEnter in your script:


public class DestroyOnExit : StateMachineBehaviour {
    public override void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
        Destroy(animator.gameObject, stateInfo.length);
    }
}

Thus you schedule object deletion just when animation has finished.

Also account for stateInfo.speed or stateInfo.speedMultiplier


animatorinspector.png (134.2 kB)
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 Goodlyay · Feb 06, 2021 at 12:39 AM 0
Share

Also make sure that "loop time" is unchecked on the .anim file you are using. Otherwise, one frame of the beginning of the animation will appear before it is destroyed.

avatar image
1

Answer by AssmeisTer · Oct 12, 2014 at 04:22 AM

A better solution that takes into account the length of the animation clip.

 Code (csharp):
  
 private int currentHealth;
  
 public void TakeDamage(int damage)
 {
     currentHealth -= damage;
     if (currentHealth <= 0)
     {
         StartCoroutine(Die());
     }
 }
  
 private IEnumerator Die()
 {
     PlayAnimation(GlobalSettings.animDeath1, WrapeMode.ClampForever);
     yield return new WaitForSeconds(gameObject, GlobalSettings.animDeath1.length);
     Destroy(gameObject);
 }
  

The code you have has StartCoroutine in Update which isn't good because it will kick off a new coroutine every frame.

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 Tyyy1997 · Mar 23, 2014 at 09:49 PM

Fixed it, this code did the trick:

 using UnityEngine;
 using System.Collections;
 
 public class ExplosionHandler : MonoBehaviour {
 
     private IEnumerator KillOnAnimationEnd() {
         yield return new WaitForSeconds (0.167f);
         Destroy (gameObject);
     }
 
     void Update () {
         StartCoroutine (KillOnAnimationEnd ());
     }
 }

I go the time from the inspector view when I selected the explosion animation in my assets, and then set it to not loop, work's like a charm.

Comment
Add comment · Show 2 · 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 njt1982 · Jun 27, 2016 at 12:01 AM 0
Share

Why is this a better answer than @firestoke's below? (curious)

avatar image hogdotmac njt1982 · May 14, 2018 at 12:12 AM 0
Share

it's not. unless you like to get hit over the head.

avatar image
0

Answer by astracat111 · Jul 07, 2018 at 09:10 PM

Just a disclaimer. myAnimatorReferenceName.GetCurrentAnimatorStateInfo(0).length gets the SECONDS, so if you need the frames like I did just do length * 60f.

P.S - I'm using timers inside of state machines instead of IEnumerators.

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.

Follow this Question

Answers Answers and Comments

37 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

2D Animation does not start 1 Answer

4.3 2D (Need help with Animator in java) 1 Answer

how to make animation wait for another animation 1 Answer

Getting with the times... learning Mechanim/Animator 1 Answer

2DAnimator Freezes first time an animation is played 0 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