Playing several animations

Im having problems, my walk and idle work fine but when i press my button to call an attack script it plays a second but cuts off the rest of the animation.

	if ( moveTouchPad.position.x > 0)
	 transform.rotation = Quaternion.Euler(0,90,0);
	animation.CrossFade("walk");
	
	if ( moveTouchPad.position.x < 0)
	transform.rotation = Quaternion.Euler(0,-90,0);
	animation.CrossFade("walk");
	
	if ( moveTouchPad.position.x == 0 && attack == true)
	animation.CrossFade("idle");

function OnGUI() {
		if (GUI.Button(Rect(10,Screen.height / 2,50,50),btnTexture))
		{
		animation.Play("attack");
		}
			}

First of all you should probably remove the:

animation.Play(“Attack”) from OnGUI()

I don’t think it’s necessary to do so but that seems like an odd way to do things, I would probably set up an attack bool which turns true on button press and turns false once the attack completes. And you could add the bool logic to your button press so it only works if your attack is complete.

For example if you press button in OnGUI and it turns attackBool = true;

bool attackBool;
bool attackActiveBool;

void Start()
{
//set animation Wrap Mode for the attack to "Once"
animation["Attack"].wrapMode = WrapMode.Once;
}

void Update()
{

//after OnGUI() button press this will be true
if (attackBool == true)
{

//start attack animation
if (attackActiveBool == false)
{
gameObject.animation.Play("Attack");
attackActiveBool = true;
}

//once the attack animation completes
if(!animation.IsPlaying("Attack"))
{
attackBool = false;
attackActiveBool = false;
}
}
}

But for your specific problem I beleive Crossfade is messing things up for you. If you want to use an animation and gaurantee it plays the whole thing and looks correct visually you should probably use animation.Play() instead of animation.CrossFade().

I have always had trouble using Crossfade for anything important. While it may look much better the logic gets a little goofy especially if you are using Animation Events.

Maybe I don’t fully understand it but I feel CrossFade needs better documentation or some game engine improvements to use it correctly for important game play type stuff.

You may need additional logic to ensure no other animations can possibly play when attackBool == true