- Home /
RaycastHit: What is the difference between hit.transform.tag and hit.collider.tag, and which should I use when?
I am trying to make a player shoot a raycast to find out if it hit an enemyobject's sphere collider. They have tags distinguishing each other. What should I use and why? Which one works better? Thanks.
Answer by RoastMaster · Jul 29, 2020 at 04:51 AM
There isn't a difference. hit.transform.tag
should always be equal to hit.collider.tag
, as tags live on a GameObject, and referencing the tag from transforms and colliders is just a convenience thing.
To add a little detail, a GameObject will ALWAYS have a Transform and Always have a tag (among other things), so they're intended to be easily accessible from anywhere. By contrast, a Collider isn't guaranteed to be on a GameObject, so you still have to access them using GetComponent<>() instead.