Collision issues (OnCollision vs OnTrigger)

While OnCollisionEnter2D(Collision2D collision) requires “transform” within “collision.transform.tag” (game will not even start showing compilation error) for OnTriggerEnter2D(Collider2D collision) it doesn’t really matter if there is this word and game works fine in both cases. Why? Things have to make sense.

More details.
Game is kind of Flappy Firefly. If the bird hits the ground - our player looses (Collision2D). If the tagged items (Collider2D) collide with bird, then they move further to have the background instead of the black screen.

//player
void OnCollisionEnter2D(Collision2D collision)
    {

        if(collision.transform.tag != "Sky") //player looses if he hits anything other then sky
        {
        Time.timeScale = 0f; //pauses imediately
        Debug.Log("Game Over");
        }

vs

//collider
private void OnTriggerEnter2D(Collider2D collision)

   {
        //if(collision.tag == "Background") can also be used
        if(collision.transform.tag == "Background")
        {
        //getting width and starting position
            float widthOfObject = ((BoxCollider2D)collision).size.x;
            Vector3 position = collision.transform.position;

        
            //changing object coordinates
            position.x += widthOfObject *1.99f;        
            //setting a new position
            collision.transform.position = position;
        

        }
   }

6385458--711567--q1add.png

Please use code tags: Using code tags properly

How to report problems productively in the Unity3D forums:

http://plbm.com/?p=220

Help us to help you.

1 Like

Collider2D and Collision2D are two different types. Collider2D has a “tag” property, but “Collision2D” does not, so if you want the tag, you have to go through either the transform, rigidbody, gameObject, or collider property, all of which can access the tag (gameObject is the most direct).

Thanks for your input, although it is still not quite clear to me why “tag” is not “Property” but an “Inherited Member” for Collider2D and a “Property” for Collision2D ?

A property is a member. A member is just a characteristic of a type. Other kinds of members are fields, methods, and events. And “inherited” member is a member that is defined in a parent class rather than e class itself. In the case of tag, i believe it’s defined in Component, which is a parent class of Collider2D.

1 Like