How play an animation through collision (2D Game)?

Hello everyone! I’m new here!

I need help whit a animation. I wanna the girl from my game eat a food. I’m trying to make a collision between the food and the girl. When collide, the girl will start the animation “eat”. My animation is made whit .png images (it’s a 2D Game).

I make all the transitions:

alt text

The parameter “eat” is trigger and the transition from “Any State” to “eat” is it:

alt text

The GameObject “girl”:

alt text

And the GameObject “food”(the script is here with the name “foodScript”):

alt text

And it’s the code from “foodScript” (Java, but no problem with C#):

#pragma strict
 
var girl : Animation;
 
function OnTriggerEnter2D(Other : Collider2D)
{
     if(Other.gameObject.name == "girl")
     {
         girl.Play("eat");
 
     }
}

I don’t know why my animation doens’t start… I 'm in it for two days. I looked for answers but found nothing… So… It’s it guys! If anyone can help me, please, do it. I don’t know where i’m going wrong. It’s Unity 5.0.0f4.

You need to use the Animator for the girl, and then activate it’s trigger.
not just tell the animation “play”.

like this:

public Animator GirlAnimator;

GirlAnimator.SetTrigger("eat");

You’re using the animator, The code you’re using won’t work like that. Try this instead.

var girl : Animator;
function OnTriggerEnter2D(Other : Collider2D)
{

	if(Other.gameObject.name == "girl")
	{
		girl.SetTrigger("NameOfTrigger");
	}
}