• 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
Question by Ihabbalakho · Jun 05, 2016 at 12:50 PM · c#animationlegacylegacyanimation

How to play animation (C#)

I created an animation and animator components and putted the animation on the parameters and changed the animation to legacy and I wrote t$$anonymous$$s..

 if (Input.GetKey (KeyCode.W))
     GameObject.Find("Object").GetComponent<Animation>().Play("AnimtionName");

but it didn't work.. I need help in t$$anonymous$$s Any help would be useful, Thanks in advance

Comment

People who like this

0 Show 0
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

3 Replies

· Add your reply
  • Sort: 
avatar image

Answer by Danisuper · Jun 05, 2016 at 02:15 PM

You should use the animator,wich is automatically created when you make an animation for a gameobject. Then in your script you'll write

  if (Input.GetKey (KeyCode.W))
      GameObject.Find("Object").GetComponent<Animator>.SetTrigger("PlayAnimation");

Watch t$$anonymous$$s tutorial about the animator for more infos. Hope it helped ;)

Comment
SandipVala
Ihabbalakho

People who like this

2 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 Ihabbalakho · Jun 15, 2016 at 09:04 AM 0
Share

@Danisuper Sorry for the late response, I had some work to do. Anyway, I just tried this but it didn't work. I just have one question, Do I have to write the animation name or the animator name? after SetTrigger.

avatar image Danisuper · Jun 15, 2016 at 04:14 PM 0
Share

You must use the animator. The rectangles are the the animations,then you must link them with an arrow. In this arrow you must choose one or more conditions, wich can be triggers,bools,floats or ints. When the conditions occur the animation is played/changed depending on the structure of your animator,so in the GameObject.Find("Object").GetComponent.SetTrigger("a trigger" ); you must first write in the animator a trigger parameter then click on the arrow between your animations (the ones you want to change,for example idle animation--->walk animation),add a condition and click on the trigger you just created. So for example, you have and idle and a walk animation. To go from idle to walk you must first add a trigger parameter,give it a name,for example "walk" and link the rectangles of the animations with an arrow, Then in the arrow add a condition and set it to be the trigger you just created. now in your code if (Input.GetKey (KeyCode.W)) GameObject.Find("Object").GetComponent<Animator>.SetTrigger("walk"); ` you must enter in SetTrigger the name of the trigger you just created wich is "walk" in this case,the condition we created in the animator is true so the there will the animation. Hope it helped you

avatar image Ihabbalakho · Jun 24, 2016 at 04:24 PM 0
Share

@Danisuper Thanks, you really helped me, now I understand the animator tab a little bit more. But why do I have to use SetTrigger? bool should be easier, you can just make a bool parameter and make it play the walking animation when true, and play the idle animation when false. But it didn't work when I used bool. Can you please show me how to use the bool parameter, and the SetTrigger worked but I can't understand how it work.

avatar image

Answer by pronobroy · Jun 25, 2016 at 05:35 AM

For play an animation first you should make an animator controller and then you can paly animation by script

animator anim; if (Input.GetKey(keycode.A) anim.setbool ("name of the animator state" , true )

note that 1st parameter is for name of state and 2nd one is for bool value.

Comment
SandipVala
Ihabbalakho

People who like this

2 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 SandipVala · Jun 25, 2016 at 09:17 AM 0
Share

Also you add trigger ,float value to play animation clip.

avatar image

Answer by JacksonPeeven · Jun 05, 2016 at 02:15 PM

Hey @Ihabbalakho I would love to help! Okay so the issue with what you are doing is that you are attempting to directly modify the component. Unity doesn't like t$$anonymous$$s and requires you to make a reference of the component first as a variable, and then play the animation from that variable.

Example:

var gameObjectAnim = GameObject.Find("Object").GetComponent<Animation>(); gameObjectAnim.Play("AnimationName");

I hope t$$anonymous$$s helped! If you have any other questions I would be glad to help!

Comment

People who like this

0 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 Ihabbalakho · Jun 15, 2016 at 09:00 AM 0
Share

@JacksonPeeven Sorry for the late response, I had some work to do. Anyway, I have created a public variable and changed it to the object that I want animate but unfortunately it didn't work. and that doesn't change anything by the way because it works just like the GameObject.Find(); Thanks anyway.

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

181 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 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 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 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 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

Legacy animation not playing - no errors? 1 Answer

How do I play legacy animations properly in Unity C# 1 Answer

Animation based on replacing sprites in sprite renderer does not work? 0 Answers

2D legacy animation. change child sprite renderer won't work 1 Answer

Distribute terrain in zones 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