Play An Animation When The Player Comes Within A Certain Distance

I need to code a script that has my target object change animations when the player comes within a certain distance. However, the object isn’t changing to the specified animation. Here’s my script. The ‘Target’ variable is the object I’m trying to get to change its animation. Thanks for any and all help!

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

public class AnimateWhenPlayerIsNear : MonoBehaviour
{
    private Animation anim;
    public float dist;
    public GameObject Target;
    private Transform player;

    private void Start()
    {
        player = GameObject.FindWithTag("Player").GetComponent<Transform>();
    }
    void Update()
    {
        dist = Vector3.Distance(player.position, Target.transform.position);
        if (dist < 5.0f)
        {
            anim.Play("New Move");
        }
    }
}

Instead of using Animation you should use Animator. Also you are never assigning the variable “anim” to anything. Either change it to public and then reference the correct animator, or (if this script is attached to the object with the animator) just use anim = GetComponent<Animator>(); in your Start or Awake function. I would suggest reading up on Animator.Play to make sure you’re using it right.

Just an added note, I’m using Mecanim.