If you search for how to compare a tag in Unity, you’ll find two ways to do this. Both work, but one may be more efficient in the long run.

Turns out object.CompareTag(“Tag Name”) is more resource efficient than object.tag == “Tag Name”

Doesn’t matter so much in small project, however, it will be more noticeable as a project size increases, according to reddit author ‘mp3-file’:

“Compare tag is faster and don’t allocate memory. Unity say: ‘“’Another unexpected cause of heap allocations can be found in the functions GameObject.name or GameObject.tag. Both of these are accessors that return new strings, which means that calling these functions will generate garbage. Caching the value may be useful, but in this case there is a related Unity function that we can use instead. To check a GameObject’s tag against a value without generating garbage, we can use GameObject.CompareTag().’’”

Ref: https://www.reddit.com/r/Unity2D/comments/b10gdo/comparetag_vs_othertag/

--

--