Animation will not play when I call it via script

I made a script that is supposed to play an animation when I press the “e” key.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DrawerInteract : MonoBehaviour
{
    public GameObject drawerOpen;
    private Animation drawer;

    void Awake()
    {
        drawer = gameObject.GetComponent <Animation> ();
    }
   
    void Open()
    {

        if (Input.GetKeyDown ("e"))
        {
            drawer.Play();
        }
    }

The script appears to compile but the animation is not playing when I press the “e” key in-game. What is wrong with the code?

There’s two ways to use GetKeyDown. There’s the KeyCode version:

Input.GetKeyDown (KeyCode.E))

which checks for the actual E key. Then there’s the string version:

Input.GetKeyDown ("jump"))

Which checks if the button with that name (here “jump”) is pressed. Those names are defined in the Input Manager.

You probably want to just use the keycode version!

Thanks a lot, it worked!