- Home /
Let's Discuss Efficiency (Tags Vs Variables)
Hey guys I was wondering... So I doubt it matters but I am using Javascript and I wanted to know if its better to refer to anything in your scene by tags or variables? I assume tags get straight to the point but your inspector gets cluttered really easy. Im assuming Unity searches for every tag in there to find it. Now for variables, does unity already know where to look when assigned? It also keeps the inspector cleaner and the code can be used far more frequently with similar things vs tags. I am not too sure guys, let me know what you think and support it with facts please.
Answer by mattssonon · Nov 12, 2013 at 08:18 AM
Setting a public variable and accessing it that way in a script is much faster than using Find()
or the faster FindWithTag()
. Unity recommends this approach in their documentation:
For performance reasons it is recommended to not use this function every frame Instead cache the result in a member variable at startup or use GameObject.FindWithTag.
You should avoid calling this function every frame eg. MonoBehaviour.Update for performance reasons. A common pattern is to assign a game object to a variable inside MonoBehaviour.Start. And use the variable in MonoBehaviour.Update.
So, to sum up, using the Find
methods is okay, as long as you don't do it at performance-crucial moments in your game, e.g. Update().