I have a file structure like:
- Ball (CircleCollider 2D + RigidBody 2D) (BallScript.cs) as parent
- BallGraphics (image + animations) as child
- Pipe (BoxCollider2D + RigidBody2D)
With this structure the ball collides the pipe successfully. It also recognises the function OnCollisionEnter2D
(prints log).
However, when I try to use the animator to change the state of the animation on collision, it doesn't work.
public class BallScript : MonoBehaviour {
Animator animator;
void Start () {
animator = GetComponent<Animator>();
}
public void OnCollisionEnter2D(Collision2D collision) {
print ("A"); // prints
animator.SetTrigger ("BallHit"); // if comment it out everything works, if not, error below..
}
}
There is no 'Animator' attached to the 'Ball' game object, but a script is trying to access it. You probably need to add a Animator to the game object 'Ball'. Or your script needs to check if the component is attached before using it.
Fair enough, but I have the ball states setup in the Animator on BallGraphics like:
other way round (hit->idle):
What is my problem? I tried tweaking bits but no success. What am I doing wrong?