October 24, 2018
Comparing tags is a thing we all do in every game project at some point but not everyone seems to know about the existence of
CompareTag. Unity could really have done a better job documenting this useful method.
In short, use
obj.CompareTag("someTag")instead of string comparisions such as
CompareTagis much faster and will log an error if a tag is not defined, which is a very good thing.
In case of comparing against a list, use
list.FirstOrDefault(t=>obj.CompareTag(t))!=nullrather than something like
list.Contains(obj.tag)for the same reasons.