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.

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!

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).

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