Tags, strings and heap allocation

Will the line if(raycastHit.collider.tag == "Pickup") cause heap allocation? Is it building up garbage?

I read in some thread where someone said it does, while PirateNinja said it doesn’t. Something in my code is allocating heap memory according to the internal profiler. The complete code is

if(direction == Globals.North) {
   if(Physics.Raycast(transform.position, direction, out colliderHit, 0.5f)) {
      if(colliderHit.collider.tag == "Pickup") {
         Move();
      } else if(passObstacle) {
         MoveOverObstacle();
      } else if(colliderHit.collider.tag == "Water"  walkOnwater) {
         MoveOverObstacle();
      }
   } else {
      Move();
   }
}

(Yes, the code will be revised and cleaned up soon)

If I spam the button when there is no collider in the way the heap doesn’t move. When there is something in the way it begins too move though, so there is something going on in the code posted.

Oh, and the functions Move and MoveOverObstacle do the same thing (pretty much) and since no heap allocation takes place when moving around those aren’t taking part if the evilness.

I tried a
private const string pickup == "Pickup"; and used that instead of the string in the if-statement. Heap allocation still happenend.

tag etc will retrieve the property and return a copy of it.
As such it will always generate garbage.

you are better of missusing layers if you need “group of objects” (or create an own manager class) or directly stored references for single object comparision (that object can naturally be searched in Start through its tag to be cached then)

On the iphone you generally need to cache as much as possible.
Only retrieve in realtime what you need to have in realtime

Right. No Tags then. Thanks for the tips.