# Unity5x GetComponent<Animation>().Play(“sequence”.StopAll) not working

I'm working with Unity3D 5x using C#. I've attached collision detection to the character controller script that plays a "gothit" animation clip (using legacy animations).

Here are the code snippets, the first is for the collision:

void OnTriggerEnter (Collider other)
{
if(other.gameObject.CompareTag ("dmgEnvironment")||other.gameObject.CompareTag ("dmgPlayerHit"))
{
GetComponent<Animation>().Play("gothit", PlayMode.StopAll);
GetComponent<Animation>().wrapMode = WrapMode.Once;
print("hit damage");
}
}


The attack animation call:

public void AnimatePunch()
{
DidAttack ();
//play animation
if (canHit && lastAttackTime < lastAttackButtonTime + attackTimeout) {
SendMessage("DidAttack", SendMessageOptions.DontRequireReceiver);
}
GetComponent<Animation>().Play("punch", PlayMode.StopAll);
GetComponent<Animation>().wrapMode = WrapMode.Once;
}


The problem I'm running into is that the "gothit" sequence won't play on collision but will do so when wired to the AnimatePunch().

I'm not sure why this is happening. Any help is greatly appreciated.

## 1 Answer

It's a fairly simple fix...just had to add animation speed & it worked.

• Can you provide more information about this solution? – Kelly Thomas Sep 6 '15 at 9:12
• Not really sure what was going on, but I had to add GetComponent<Animation>()["gothit"].speed = 3.5f; to make it work – Mark Laurel Sep 6 '15 at 15:44
• You probably should mark it as answer. – Maxim Kamalov Sep 8 '15 at 12:12