- Home /
Is CompareTag faster than checking directly the GameObject?
Raising the assumption that i have some objects moving around in a FixedUpdate and only care if they collide with a specific object.
If this object has been set with a custom Tag, and the others had a GameObject variable holding the reference to it, in the case of a collision, what would be faster or less resource consuming:
Use CompareTag to compare the tag string or make a direct comparison using the stored GameObject reference ?
I've been making some profiling, but would appreciate the opinion of someone with a little more experience.
Answer by Bunny83 · Jun 06, 2019 at 07:53 PM
We don't know exactly how CompareTag is implemented on the native side. However since tags are actually strings it would require either a string comparison (that would also be true when a dictionary lookup is used internally since it would be necessary to create a hash of the passed string).
So a direct reference comparison would always be faster as long as you don't need any GetComponent calls, though even then if the gameobject only has a few components, GetComponent might also be slightly faster than CompareTag I guess.