Ordinarily, retrieving a string property from an object is the same as retrieving any other reference type property in C#; it is acquired with no additional memory cost. However, for whatever arcane reason hidden within the Unity source code, retrieving string properties from GameObjects duplicates the string in memory and results in a heap allocation. This draws the attention of the Garbage Collector, which, if we are not careful, can cause CPU spikes that will affect performance during runtime.
The two properties of GameObject affected by this strange behavior are tag
and name
. Retrieving either of these properties for any reason will cause unnecessary heap allocations. Therefore, it is unwise to use either property during gameplay, and you should only use them in performance-inconsequential areas such as Editor Scripts. However, the Tag system is commonly used for runtime identification purposes, which can make this a significant problem...