Playing animation for certain keys

I want to move my character with animation, but it does not work with this animation script.

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

public class Animation : MonoBehaviour
{
    public Animator anim;
    // Start is called before the first frame update
    void Start()
    {
        anim = GetComponent<Animator>();
    }

    // Update is called once per frame
    void FixedUpdate()
    {
        if (Input.GetKey(KeyCode.LeftArrow)){
		    anim.Play("moveLeft");
	    }
        if(Input.GetKey(KeyCode.RightArrow)){
            anim.Play("moveRight");
        }
        if(Input.GetKey(KeyCode.UpArrow)){
            anim.Play("moveForward");
        }
        if(Input.GetKey(KeyCode.DownArrow)){
            anim.Play("moveDown");
        }

    }
}

Hello @yi900629. You didn’t assign your animator reference the right way.

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

public class Animation : MonoBehaviour
{
public Animator anim;
void Start()
{
//the issue:
anim = GetComponent();
}

The line I commented out should be anim = this.GetComponent<Animator>();
Another issue is that you don’t have an Animator component attached to the player. Make sure to fix these issues and it should work.
I hope that was helpful, and if you need any further assistance, just comment or message me. :slight_smile: