Button press for Animations

I want to be able to initiate the “attack” animation upon button press.
But I can not quite get it to work with the script I am using. I recieve the build error message:


“The type or namespace name ‘function’ could not be found (are you missing a using directive or an assembly reference?) (CS0246) (Assembly-CSharp)”


How can i fix this problem to have the button initiate the animation.

using UnityEngine;
using System.Collections;

public class animator : MonoBehaviour 

{
	function Update ()
	{
		if ( Input.GetButtonDown("Attack") ) {
			transform.animation.Play();
		}
	}

		
	}

Hi, I think you have several mistakes here. first things first:

The build error you are getting is because to declare a function in C# you don’t use “function” that’s for javascript, so to declare correctly the Update function it should be like:

void Update ()  {
/* Your code here **/
}

The second error you have there is in the Input.GetButtonDown("Attack"). that’s never going to work because that function is for capturing keyboard events or mouse events and things like that, like for example “if key ‘a’ is pressed do something”. to make that button work you’ll have to bind a method to it’s event. Take a look at this video to learn the basics about UI and events:

https://unity3d.com/es/learn/tutorials/modules/beginner/ui/ui-events-and-event-triggers

once you have fixed it you have to add a variable to the animator. Let’s name it attack and make it “Trigger”. then instead of using transform.animation.Play(); you use animator.SetTrigger("attack"); the “animator” I put before should be the Animator component of the game object, to obtain it you have to use the getComponent(); function here You have another link where it explains further the use of variables inside the animator

It’s quite a long reply but I think you have here all you need to make that work. If you have any doubts about it ask ;).