• 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
3
Question by Teadaddy · Apr 16, 2015 at 08:27 PM · mecanimscriptingbasics

UI button trigger animation

I have my animation clips and controller set up right, the animation plays in game. Now I am trying to figure out how to trigger it only when a UI button is pressed. Any help? C# please.

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

3 Replies

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

Answer by Addyarb · Apr 16, 2015 at 11:00 PM

First, open up your animator and create a new bool called "Run" or something you'll remember.

Next, write a script such as this, and place it on the character.

  public void PlayAnimation() {
     GetComponent<Animator>().SetBool("Run",true);
     }

Now, make a new UI button and click the "+" button to add a new OnClick function.

Next, drag your player into the GameObject prefab slot, click on the dropdown menu at the bottom, find your script's name, and go down to the function you made (For instance, it would be called PlayAnimation() in this case).

Comment
Add comment · Show 5 · 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 DA_ATeam · Aug 20, 2015 at 08:57 AM 0
Share

Thanks Addyarb! This is just what I've been searching for. I just knew it wouldn't take a whole page of code. When I click on the button, nothing happens though. I'm running Unity 5.1! Compatibility problems? I have a character with the PlayAnimation script. He has an Animator state with a Bool perameter,(Throw). I set up the the function on the button just as you instructed. I've got nothing when I click! ! Here's my code that's on the character.

 using UnityEngine;
 using System.Collections;
 
 public class Btn : $$anonymous$$onoBehaviour {
 
 
     public void PlayAnimation() {
         GetComponent<Animator>().SetBool("Throw",true);
     }
 
 }

In the inspector, the Button Script 'OnClick" has the BtnPlayAnimation function highlighted. I've dragged the Character prefab into the GameObject slot.As always any help would be much apprecriated. Thanks in advance.

avatar image Teadaddy · Aug 20, 2015 at 05:00 PM 0
Share

Hi d1dOnly- just to be sure, you have the animator controller set up with the animation in it?

avatar image DA_ATeam · Aug 21, 2015 at 04:29 AM 0
Share

Thanks Teadaddy! Yes! The "Throw" is in the animator controller slot.

avatar image Skypea · Aug 29, 2016 at 06:38 AM 0
Share

Hi, does it also work with the $$anonymous$$ainCamera ins$$anonymous$$d of the player?

Because I tried this instruction with the $$anonymous$$ainCamera (I have no player), but it does not work :(

avatar image geeemm218 · Sep 01, 2019 at 12:20 PM 0
Share

Does this work with UI panels as well? Once I start the program, the animation plays immediately even though I haven't clicked on the button yet.

avatar image
4

Answer by Axion92 · Jul 20, 2017 at 03:46 PM

To anyone else who stumbles in here looking for answers, in Unity 5.6 you can simply put the object you're animating into the buttons OnClick event, and then access the animator in the dropdown to select SetTrigger(string).

Then just input the name of the animation as a string and the button will trigger the animation when pressed!

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 andrefbatista · Nov 09, 2017 at 03:39 AM 0
Share

Thanks a lot man!!! I'm a noob with coding, so this saved my life!! Just a small correction, you said to input the name of the ANI$$anonymous$$ATION, but only works with TRIGGER name.

avatar image matthew77 · Jan 31, 2018 at 06:05 PM 0
Share

Hi. I'm trying to do this also but when I do it I get an error that says parameter "name of my animation" does not exist. Any idea what I'm doing wrong?

avatar image ehkoda · Apr 15, 2018 at 06:02 PM 1
Share

Thanks a lot!!! Only one thing, i'm having trouble finding the setTrigger function. Can you help me?

And how to I prevent the animation from playing on awake?

avatar image ShadySaberMohamed · Jun 10, 2019 at 06:29 AM 0
Share

But what if I need to attach button to make animation to player that making also many animations with a SetInteger

avatar image
1

Answer by moriggi · Aug 20, 2015 at 10:39 PM

before the PlayAnimation function you need to make a reference, so write Animator animator; animator = GetComponent().SetBool("Throw",true);

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 DA_ATeam · Aug 21, 2015 at 04:26 AM 0
Share

Thanks! I'll give it a try.

avatar image DA_ATeam · Aug 21, 2015 at 05:54 AM 0
Share
 public class Btn : $$anonymous$$onoBehaviour {
 
     Animator animator; 
 
     public void PlayAnimation() 
 
         {
             animator = GetComponent<Animator>().SetBool("Throw",true);
     }
 
 }


I get the following console error:

Assets/Btn.cs(11,25): error CS0029: Cannot implicitly convert type void' to UnityEngine.Animator'. Then I tried...

 public class Btn : $$anonymous$$onoBehaviour {
 
     Animator animator; 
 
     public void () 
 
     {
             animator = GetComponent<Animator>().SetBool("Throw",true);
             PlayAnimation("Pass");
     }
 
 }




That didn't work either. Drats! I think we're getting closer though. I got he following error: Btn.cs(8,21): error CS1519: Unexpected symbol `(' in class, struct, or interface member declaration! Thanks!

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

28 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

Related Questions

learn to scripting parameter in Mecanim 1 Answer

Mecanima Scripting Animations 2 Answers

AnimatorStateInfo In Multiple Scripts 0 Answers

Mecan and root rotation in X, Z axis (pitch and roll) 2 Answers

There were some problems with his fingers in mecanim 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