Using Tags with OnCollisionEnter2D

I saw in another thread that in order to use tags with OnCollisionEnter2D you have to utilize other.GameObject.tag == “Example” whereas OnTriggerEnter2d is simply other.tag == “Example”.

Is there an explanation as to why there’s a difference? I’m having trouble understanding why.

Hi Boxy,

The explanation is fairly simple: because the Unity programmers did it this way. :wink:

How do we know that we have to use one or the other? By checking the API.

The parameter of the OnCollisionEnter2D method is of type Collision2D. When checking the Collision2D class, we can find a list with all accessible members. As you can see, there is no tag property in the list. However, we know that the GameObject class contains that property, thus we access the GameObject object via gameObject, which is in the list. Alternatively, we could have accessed transform.

And we do the same for the OnTriggerEnter2D method.

The classes and their content are not a natural law. They were created by the Unity programmers. That’s why we have to take a look at the API if we want to know any details.

Did this help? :slight_smile:


See also:

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms