Weapon Shooting Animation not playing

Here is the code

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

public class GunScript : MonoBehaviour
{
   
    [SerializeField] float Damage = 10f;
    [SerializeField] float range = 100f;
    [SerializeField] Camera Cam;
    [SerializeField] Animator WeaponAnimator;
    [SerializeField] AudioSource ShootSource;
    [SerializeField] AudioClip ShootSound;
    [SerializeField] ParticleSystem MuzzleFlash;
    [SerializeField] bool IsAutomatic;
    bool isfiring = false;
    [SerializeField] string ShootAnimation;
    [SerializeField] string IdleAnimation;
   
    void Update()
    {
        WeaponAnimator.Play(IdleAnimation);
        if (Input.GetMouseButtonDown(0))
        {
            Shoot();
        }
    }

    void Shoot()
    {
        RaycastHit Hit;
        if (Physics.Raycast(Cam.transform.position, Cam.transform.forward, out Hit, range))
        {
            Debug.Log(Hit.transform.name);
        }

        ShootSource.PlayOneShot(ShootSound);
        MuzzleFlash.Play();
        WeaponAnimator.Play(ShootAnimation);
    }
}

I have tried putting WeaponAnimator.Play(IdleAnimation) under Void Start and it does, but if I do that, then it screws up the weapon switch code by only playing the “Idle Animations” once. I also do not have any of the idle animations on loop.

You should really use the Animator State machine to control your animation transitions. Then you can make the shooting animation play just by setting a trigger in code and the state machine will take care of playing the shooting animation and transitioning back to idle.

Using Play causes all kinds of issues like this.

I have already messed with triggers but the animation wouldn’t play