OnCollisionEnter2D issues in Unity 5.2

At this point I’m desperate. I just can’t get that thing to work.
I’ve got two prefabs initiated through code

void Start()
    {
        first = Instantiate(Resources.Load("poke"), new Vector3(), Quaternion.identity) as GameObject;
        second = Instantiate(Resources.Load("poke"), new Vector3(), Quaternion.identity) as GameObject;
        first.AddComponent<Rigidbody2D>();
        second.AddComponent<Rigidbody2D>();
        first.AddComponent<CircleCollider2D>();
        second.AddComponent<CircleCollider2D>();
        first.gameObject.GetComponent<Rigidbody2D>().gravityScale = 0;
        second.gameObject.GetComponent<Rigidbody2D>().gravityScale = 0;
}

Both of them now have a rigidbody and a collider. Triggers are of. kinematic property is off. Gravity is set to zero. When the objects collide, they act like they should, bumping into eachother but the event won’t fire.

 void OnCollisionEnter2D(Collision2D collision)
    {
        Debug.Log("collision");
    }

I tried moving the objects by transforming their position and by adding Force to them.
What is going on?

I tried your exact code and everything worked as expected. Some assumptions I made:

  • The OnCollisionEnter2D method is in a seperate script attached to your poke prefab.
  • The thing that your poke prefab is colliding with is also a 2D collider.

If you move your objects by transforming them, the isKinematic bool gets set to true while that’s happening, so you’re better off adding force in this case.

Also, have you considered using OnTriggerEnter to see if that would work? There’s a really good breakdown of when to use what here:

I can never keep it straight in my head though, so I just end up trying them both to see what works best for my setup.