x


Is CompareTag better than gameObject.tag performance wise?

Hello,

Someone told me that this:

 function OnTriggerEnter (other : Collider) {
     var deathMenu = GetComponent("");
 
     if (other.CompareTag("Lava")) {
         
     }
 }

Is better than this:

 function OnTriggerEnter (other : Collider) {
     var deathMenu = GetComponent("");
 
     if (other.tag == "Lava") {
         
     }
 }

Is that true? Thanks.

more ▼

asked Jan 01, 2012 at 04:44 PM

tatelax gravatar image

tatelax
170 61 942 61

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

CompareTag compiles to fewer CIL instructions than the direct comparison, for sure: it probably results in a single call to the CompareTag internal function, while the other alternative will need a call to the tag getter function (to get the tag string) and another call to the string comparison function, all of this in CIL (the interpreted language used in .NET applications). Since the tag string must be returned by the getter function, space must be allocated, what takes extra time. Anyway, the difference probably is very small, but may become significant if a lot of CompareTags is used in Update or OnCollision/TriggerStay, and/or the target machine is a mobile device.

more ▼

answered Jan 01, 2012 at 05:19 PM

aldonaletto gravatar image

aldonaletto
57.9k 33 96 310

So, why would someone ever use '.tag' other than to use the result in a switch statement?

Mar 16 at 01:44 AM Rxanadu

I use it for assigning tags in editor for example.

Jun 19 at 10:40 AM Amitloaf
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x572
x85

asked: Jan 01, 2012 at 04:44 PM

Seen: 4022 times

Last Updated: Jun 19 at 10:40 AM