0

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:

enter image description here enter image description here

other way round (hit->idle):

enter image description here

What is my problem? I tried tweaking bits but no success. What am I doing wrong?

M. Schena
  • 2,039
  • 1
  • 21
  • 29
senty
  • 12,385
  • 28
  • 130
  • 260
  • You can try GetComponentFromChild. – Paweł Marecki Feb 02 '16 at 08:29
  • I couldn't use GetComponentFromChild but I used `animator = GetComponentInChildren();` instead. It works now (I mean doesn't crash), however now I started receiving another error: `'BallGFX' AnimationEvent has no function name specified!` – senty Feb 02 '16 at 08:33
  • 1
    Check if there is an event added to animation. Looks like there is an event but its empty, there is no function attached. Maybe a missclick. – Paweł Marecki Feb 02 '16 at 08:42
  • It was pretty small so i missed it. Cheers :) – senty Feb 02 '16 at 09:30
  • Please delete this question, to help avoid the incredible clutter with the Unity3D tag. – Fattie Feb 02 '16 at 15:42

0 Answers0