Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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
0
Question by Gusic · Jul 14, 2015 at 04:56 PM · c#animationplatformer

Simple animation script not working (C#)

Hi I have a script for when my player is hit by a bullet he plays a dying animation then the object is destroyed. But the script isn't working i have added an animator to the player but still nothing happens. all that happens is the object getting destroyed and the animation not playing.

CODE

 public GameObject player;    
 public Animator animation;
 
         void Start () {
         
         animation = GetComponent<Animator> ();
         
     }
 
     void OnTriggerEnter2D(Collider2D col)
     {
         if(col.gameObject.tag == "Bullet")
         {
             animation.Play("Dying");
             Destroy(player);
 
 
         }
     }
 

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 thisisaunityaccount · Jul 14, 2015 at 05:14 PM 0
Share

You're destroying the player the instant the animation starts playing. Change your code so the object is destroyed after the animation is finished.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by wesleywh · Jul 14, 2015 at 07:05 PM

So I take it that you want the animation to play THEN have your player destroyed. Just like it has been already said your immediately destroy your player after you start you animation. For all I know your animation could be playing but you destroy your player so fast you will never see it. Simply add a delay before you destroy your player or check to make sure your animation is done or in a certain state.

Comment
Add comment · Show 4 · 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 Gusic · Jul 14, 2015 at 09:13 PM 0
Share

How would i delay it

avatar image wesleywh · Jul 14, 2015 at 09:32 PM 0
Share
 void Start () {
      animation = GetComponent<Animation> ();//notice this isn't Animator, there is a big difference between the two 
 }
 void OnTriggerEnter2D(Collider2D col){
      if(col.gameObject.tag == "Bullet")
      {
          StartCoroutine(playDeath(5.0f));
      }
 }
 IEnumerator playDeath(delayTime:float){
      animation.Play("Dying");
      yield return new WaitForSeconds(delayTime);
      Destroy(player);
 } 

So do you want to use Animator(new $$anonymous$$echam Animations) or Animation(Legacy Animation)?

avatar image Gusic · Jul 14, 2015 at 09:34 PM 0
Share

animator. Where do i define the delayTime

avatar image wesleywh · Jul 14, 2015 at 09:41 PM 0
Share

I fixed my code. Wrote it wrong. Anyway, you can't use "animation.Play". You will have to setup animation states and use the following in your code:

 anim.SetBool("isDead", true);//this is simply true go to the dead state from your current state

Or something equivalent to it. Unity has a very good tutorial going very in depth about this. The one that I learned from is here(Its out of date, unity 4 but still applicable):

https://www.youtube.com/watch?v=Xx21y9eJq1U

Or from the live training one that is found here:

https://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/animate-anything

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

22 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

Related Questions

How do I know when an object is going under a Platform Effector 2D object? 0 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How to add keyframe animation to an imported FBX. 0 Answers

Animation at the end of the level 1 Answer


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