FPS Animation Script

I am making a script so if you press a button, a certain animation plays, but i want to play the variables animation, not making it so if i want to change the animation i have to go inside the script and change it. When scripting i noticed i didnt know what i was doing wrong, can someone help me?

var walk : animation;
var sprint : animation;
var shoot : animation;
var idle : animation;
//This is for the walk animation
if (Input.GetButtonDown ("W")) {
animation.Play(walk, PlayMode.StopAll);
}

//This is for the sprint animation
if (input.GetButtonDown ("leftshift")) {
animation.Play(sptint, PlayMode.StopAll);
}

//This is for the shoot animation
if (input.GetButtonDown ("fire1")) {
animation.Play(shoot, PlayMode.StopAll);
}

//This is for the idle animation
if (input.GetButtonDown ("")) {
animation.Play(idle, PlayMode.StopAll);
}

} else {
animation.Play(idle, PlayMode.StopAll);

There's two ways you can do this:

    var walk : Animation;
    var sprint : Animation;
    var shoot : Animation;
    var idle :Animation;
    
    //This is for the walk animation
    if (Input.GetKeyDown(KeyCode.W)) 
    {
    walk.Play();
    }
     
    //This is for the sprint animation
    if(Input.GetKeyDown(KeyCode.LeftShift))
    {
    sprint.Play();
    }
     
    //This is for the shoot animation
    if (Input.GetButtonDown ("Fire1"))
    {
    shoot.Play();
    }
     
    if(Input.anyKey == false)
{
idle.Play();
}

And drag you animations into the proper places in the inspector where it says “walk, sprint, idle, shoot”.

Or you could try:

  var anim : Animation;
        
        //This is for the walk animation
        if (Input.GetKeyDown(KeyCode.W)) 
        {
        anim.Play("walk");
        }
         
        //This is for the sprint animation
        if(Input.GetKeyDown(KeyCode.LeftShift))
        { 
        anim.Play("sprint");
        }
         
        //This is for the shoot animation
        if (Input.GetButtonDown ("Fire1"))
        {
        anim.Play("shoot");
        }
         
        if(Input.anyKey == false)
    {
    anim.Play("idle");
    }

Which would play your animations based on their name, and I think you’d have to add them to the animation component on your player. I don’t know if it’s possible to do what you’re going, but I would lean towards saying no.