.tag doesn't work but gameObject.tag does

In this lecture we again use other.tag

This worked for me in a previous course project but now it gives me an error saying “Collision2d doesn’t contain definition for “tag””

But other.gameObject.tag works. What is causing this?

Hi,

Have you already checked the API? The classes, methods and variables we use are written by other people, unless we wrote them ourselves. And if they didn’t declare a public variable named tag in the Collision2D class, we cannot access the non-existing tag.

I do not know why they defined the tag in Collider2D but not in Collision2D. For the sake of consistency, it would have made sense to have that field in both classes. However, we cannot apply much logic here because, in the end, the classes were written by people who have their own way of thinking. Maybe there were different programmers who didn’t know about the respective other class.

That’s why it is usually a good idea to take a look at the API if something does not work as expected.


See also:

This is propably the reason as I did colliision checks differently than Rick as it was challenge and I didn’t change my way since it worked.

It would seem that OnTriggerEnter has tag method but OnCollisionEnter perhaps doesn’t. Slight difference.

Challenging yourself is absolutely fine. If your code solves the problem, feel free to keep it. :slight_smile:

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

Privacy & Terms