How do i make an animation play on key press?

Hi guys this is my first post. I would very much like to know how i can make an animation play when i press a key. I started using unity about 3 days ago so talk to me like im stupid and with detail. i tried making a script and this is what i have:

        function Update()
      // Plays the walking animation - stops all other animations
      animation.Play("walking", PlayMode.StopAll);


im almost 100% sure that is correct but when i start the game the animation starts playing without me pressing anything. I have dragged the script to the object that i want to animate in the Hierarchy and i made sure the names are correct. I an also getting this error:

MissingComponentException: There is no ‘Animation’ attached to the “1FPS_HAND” game object, but a script is trying to access it.
You probably need to add a Animation to the game object “1FPS_HAND”. Or your script needs to check if the component is attached before using it.
walkingscript.Update () (at Assets/Anims/Walking/walkingscript.js:6)

What does that mean??? how do i add an animation, i already have an animation. And i know this because when i select “1FPS_HAND” on the animation window, i can see the animation is there. But i dont see an animation section in the inspector panel but i see animator. Can anybody help me with this? Im pretty sure its not the script thats wrong. Let me know if you need more info. THANK YOU GUYS!!!

I’m going to assume you have an Animator attached to your object and it is taking the flag and running. I’m not even sure that the way I handle this is the best but I’ll invite you to explore my solution to this problem.

Open up the Animator window. This can be found at the top under “Window” and “Animator” (I believe it has no hotkey associated with it.) When this opens, select your Game Object in your Hierarchy. If the Animator has an orange box with the name of your animation in it, this is why it is auto-animating. The way that I fix this is that I right click inside the Animator window and create a new state and I call it Idle. Inside Idle I don’t assign any animations at all. Right click Idle and set it to the default state, which should then make it orange. From here you want to right click on Idle again to create a transition, at this point an arrow will appear underneath your mouse cursor and you will want to use it to click on your other animation state (the one that was originally orange.) This tells the Animator you want to go from Idle (where no animation is playing) to a state where an animation is playing. In the bottom left of your Animator window is a box called “Parameters”, go ahead and click on the + on the rightside of that box and add yourself a Trigger which you can name anything you want. Then click on your transition arrow from Idle → Whatever Animation and check the inspector. Change the Condition from “Exit Time” to your Trigger. You will also want to set a transition FROM your Animation TO Idle so that it exits after the animation is complete (and you can use the default settings for a transition for that, no new triggers required.)

After you do that block of text, go into your code and inside the check for input write
GetComponent().SetTrigger( “NameOfYourTriggerYouMadeThatIDontKnowLol” );

… or whatever the uh Boo equivalent is? idk I’m a C# man :frowning: I think you get the picture though.

Click on the 1FPS_HAND object and go to the top of your screen to “Component>Miscellaneous>Animation” and give it an animation clip.

whats mytrigger?