• 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
0
Question by wwwboomboom123 · Jun 16, 2013 at 09:19 PM · animationenemy

can someone show me how to play a death animation when a the game object is destroyed (javascript)

Basically i need help with getting my enemy in game to play a death animation when the game object is destroyed.

Comment
Add comment · Show 1
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 InfiniBuzz · Jun 16, 2013 at 09:28 PM 0
Share

Basically you would first play the animation and then destroy the gameObject. You can write a custom destroy method (function) that plays an animation and then destroys the GO as soon as the animation finished. Tell me if you need any help with the code :) Do you have some scripts already?

3 Replies

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

Answer by InfiniBuzz · Jun 17, 2013 at 05:11 PM

try this :)

 #pragma strict
 
 var Health = 100;
 var animDie : AnimationClip; // Drag your animation from the project view in here (to inspector)
 
 function ApplyDammage (TheDammage : int)
 {
     Health -= TheDammage;
     if(Health <= 0)
     {
        Dead();
     }
 }
 
 function Dead()
 {
     animation.Play(animDie.name);
     Destroy(this.gameObject, animDie.length);
 }

hope it helps

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 wwwboomboom123 · Jun 18, 2013 at 11:58 AM 0
Share

thank you so much for that it works :)

avatar image InfiniBuzz · Jun 18, 2013 at 12:00 PM 0
Share

glad it helped :)

avatar image
0

Answer by SubatomicHero · Jun 17, 2013 at 08:28 AM

In your script somewhere you would call Destroy() like so:

 // if this script is attached to the object to destroy
 animation.Play("animation name goes here");
 Destroy(this.gameObject, animation.time);
Comment
Add comment · Show 8 · 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 wwwboomboom123 · Jun 17, 2013 at 04:30 PM 0
Share
 #pragma strict
 
 var Health = 100;
 
 function ApplyDammage (TheDammage : int)
 {
     Health -= TheDammage;
     
     if(Health <= 0)
     
     {
         Dead();
         
         
         
     }
 }
 
 function Dead()
 {
     animation.Play("dead");
 Destroy(this.gameObject, animation.time);
 }
avatar image wwwboomboom123 · Jun 17, 2013 at 04:33 PM 0
Share

thats the code btw i get an error saying." 'time' is not a member of 'UnityEngine.Animation' yeah i'm pretty new to coding.thanks for helping

avatar image InfiniBuzz · Jun 17, 2013 at 04:35 PM 0
Share

try animation.clip.time

avatar image wwwboomboom123 · Jun 17, 2013 at 04:53 PM 0
Share

thanks man yeah i'm pretty new to code do i put that where the animation.time is?

avatar image InfiniBuzz · Jun 17, 2013 at 04:55 PM 0
Share

Exactly there

Show more comments
avatar image
0

Answer by branek42 · Oct 10, 2013 at 09:04 PM

Help me pls script is not work send me instructions gagac@centrum.sk

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

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

18 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

Related Questions

Multiple Cars not working 1 Answer

Enemy animation help? 1 Answer

Wait Random time then play animation 2 Answers

How to make enemy kill you if starring at it? 2 Answers

Enemy Death and Killing Script 3 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