Hi Boxy,
The explanation is fairly simple: because the Unity programmers did it this way.
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?
See also: