- Home /
find with tag VS find with name.
Hi, i know that finding a gameobject by using its name is resource intensive as it checks every object 'till it finds it. Also know that it's a cardinal sin to put that in Update().
My question is, is it better to try and find something by its tag instead? Does it still check every object for its tag or does it have a list with the tags and checks only the objects with that tag?
thx.
Answer by mayorc1978 · Feb 23, 2014 at 07:39 PM
Yes it's better to search by Tag, it should be more optimized, moreover when you have a large number of objects in general and only few with specific tags.
If timing it's not striclty important you can define custom timing for search method, using a Coroutines with specific time interval. Update() method will do this each frame, with a Coroutine you can do it every half second or less, you choose.
Anyway remember in most case you need to find an object once, maybe in the Start() method, and then you can cache that reference and use that directly. This last case is usable for GameObjects components too. Caching and pooling is the key to optimization for scripts. And it's usually the most ignored.
Thx. Yeah i'm using find only in start, but i want to make even that as best as possible. I had a few things that are common in multiple scenes, so i wanted to find them by script (In case i forget to assign them in the editor) from the various objects that needed them. So i thought of parenting them under an empty object with a tag, finding just that by scripting in Start(), and then moving to its children.
You can even write an Object$$anonymous$$anager that checks consistency in each scene and that do the work for you, but depends on the level of complexity of the scenes, and the time needed to write that Object$$anonymous$$anager too.
Answer by Zentiu · Feb 23, 2014 at 09:51 PM
I cant be sure but look at tags as boxes like you see variables in your script.
unity already knows who has what tag when you assign it to them.
so if you have like 5 gameobjects with the "NPC" tag and 10 with the "enemy" tag and you want to look inside objects with enemy tag, he will only look through 10 gameobjects.
don't quote me on it though but personally I prefer tags.
It's also what i also understood so far, but i wasn't sure my self.
Your answer
Follow this Question
Related Questions
Is it okay to assign and 'un-assign' game object tags at runtime, will it come back to bite me? 0 Answers
ArrayLists or Tags? 1 Answer
General programming question on Static Classes 1 Answer
Performance hiccup on initial contact with rigid bodies 0 Answers
How can I easily implement performance statistics grabber? 1 Answer