- Home /
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.
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).
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.
Hi d1dOnly- just to be sure, you have the animator controller set up with the animation in it?
Thanks Teadaddy! Yes! The "Throw" is in the animator controller slot.
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 :(
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.
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!
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.
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?
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?
But what if I need to attach button to make animation to player that making also many animations with a SetInteger
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);
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
Follow this Question
Related Questions
Mecanima Scripting Animations 2 Answers
AnimatorStateInfo In Multiple Scripts 0 Answers
learn to scripting parameter in Mecanim 1 Answer