Search Unity

collision2D inconsistency! - OnCollisionEnter acts differently to 3D counterpart.

Discussion in '2D' started by stuatk, Nov 26, 2013.

  1. stuatk

    stuatk

    Joined:
    Nov 18, 2009
    Posts:
    53
    The situation:

    2 objects in the scene heading towards each other, each having a rigidbody2D, boxCollider2D and my simple script components attached to them.

    I have this function in the c# script on each object:

    void OnCollisionEnter2D( Collision2D col ){
    col.gameObject.SetActive( false );
    }

    One object works (it is deactivated in the scene) then next throws null reference exception - at first I thought, ok fair enough.
    So I added a null check before the line that deactivates the gameobject:
    if( col == null ) return;

    still throws same error! This doesnt work either: ( if( col.gameObject == null ) return; )

    Anyway... heres the point.
    By this time my suspicions were aroused - it didnt feel right, I'm sure when I've done this in the past it was fine...
    So I repeated the whole thing but using 3D objects, 3D rigidbodys and 3D colliders.

    It all worked fine first time, with none of the null object checks. Both objects disappeared when they collided - no errors reported.
    This is exactly how the 2D versions should have worked.

    Massive inconsistencies. I get the feeling the 2D stuff was released WAY to early, its buggy (I've had tediuos problems with the sprite editor) and now my time has been wasted tracking this particular problem.

    Just a heads up really. So I'll be using tried and tested 3D collision stuff from now on. Maybe in a year I'll see if the 2D stuff is finished...
     
  2. honigkuchen

    honigkuchen

    Joined:
    Nov 24, 2013
    Posts:
    7
    They should just buy TK2D Chipmunk Physics... the current 2D system is a mess :/
     
  3. KAiymu

    KAiymu

    Joined:
    Sep 2, 2014
    Posts:
    12
    Hello :).

    I had exactly the same problem, I patched it by changing the OnCollisionEnter2D(Collision col) by OnTriggerEnter2D(Collider col).

    In my case, it was arrow that needed to shot some balloons.
    If you want that to work, you need to put either one of your colliding object (My arrows or the balloons in my case) with the Trigger checked on the BoxCollider2D.