|
I have a master 'controller' object in the scene which many objects need reference to. Would tagging the object and finding it be faster than using find with object's name?
(comments are locked)
|
|
Based on this line: 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. From: http://unity3d.com/support/documentation/ScriptReference/GameObject.Find.html I would say that finding by tag is far less resource intensive. does the same go for components? or variables on others objects? or should those be global variables? Thanks in advance!
Nov 16 '10 at 01:51 PM
Matt 15
Would also be interested in knowing how Find and FindWithTag compare to using global variables, both on a speed and memory level? What's best?
Aug 22 '12 at 06:19 PM
BilboStabbins
Don't compare Find / FindWithTag with "global variables". It's something completely different. FindWithTag is probably faster than Find since, as far as i know, Unity use seperate arrays for each tag. GetComponent is usually faster than those Find methods since it only have to search on a given GameObject, but it also has a totally different usage. GetComponentsInChildren is of course worse but it depends on how big and deep your hierarchy is downwards. The general rule is: Avoid calling any of those searching functions every frame. GetComponent is not that bad, but in most cases it's not difficult to get the reference once and then just use the cached reference. When you say "global variables" i guess you talk about static varaibles? As i said above, that's something totally different. Static variables belong to the class, not to an instance and Find / Getcomponent / ... are all about instances. The "speed level" can simply be tested, but it's obvious that a direct reference is always faster than using a function that searches for an object (especially when it has to use a string compare). I'm not sure what you mean by "memory level" and what you mean by "best". You have to give two concrete samples which you want to compare. This is by far to general
Aug 22 '12 at 06:40 PM
Bunny83
Great explanation, that's making much more sense now :) What I meant by 'global variables' is, for example, if we make a GameObject public inside a script and then drag the object from the scene into its slot in the Inspector. I was wondering what the difference is between having a constant reference like this compared to a cached one using Find, and in what circumstance to use each. The question about memory refers more to this, in that whether the 'global variable' object is stored in memory throughout?
Aug 22 '12 at 06:52 PM
BilboStabbins
(comments are locked)
|
