walk animation script

i have a walk animation script that looks like this, but doesn’t quite work properly
can anyone tell me what is wrong with my code? and/or tell me how to fix this?

function Start () {
 
    animation["walkanimation"].speed = 1;
    animation["walkanimation"].wrapMode = WrapMode.Loop;
 
}
 
function Update () {
 
    if (Input.GetButtonDown("w")) {
 
       transform.animation.Play("walkanimation");
 
    }
    if (Input.GetButtonUp("w")) {
 
       transform.animation.Stop("walkanimation");  
    }
}

I find it easier to use ENUM states to handle my animations, it makes it simple to add more animations later and also makes integrating multiplayer animation easier.

Declare this

	public enum CharacterState 
	{
		idle,
		walking	
	}
	
	public CharacterState _state;
	public string idleAnimName;
	public string walkAnimName;

    void Update() 
	{	
			CheckKey();
	}

	void CheckKey()
	{
		if(Input.GetKeyDown(KeyCode.W)) {
			_state = CharacterState.walking;
		} else if (Input.GetKeyUp(KeyCode.W)) {
			_state = CharacterState.idle;
		}
		PlayAnimation();
	}
	
	void PlayAnimation() 
	{
		switch(_state)
		{
		   case CharacterState.idle:
		       	animation.CrossFade("idleanimation");
		       	break;
		
		   case CharacterState.walking:
		       	animation.CrossFade("walkanimation");
		       	break;	
		}			
	}