I seem to remember a general purpose Find(string name) type function that would search for a game object by name. Am I imagining things? I can’t find it now.
Thanks,
-Jon
I seem to remember a general purpose Find(string name) type function that would search for a game object by name. Am I imagining things? I can’t find it now.
Thanks,
-Jon
Are you looking for the FindGameObjectWithTag() and the FindGameObjectsWithTag() functions? On The Edge - Det bedste fra tech verdenen
No, it was just GameObject.Find(“Name Of Object In Your Scene”)
I think it’s still there, but not documented. Perhaps there is a reason why I should not use it.
I realize there are other approaches I could take, I would just prefer this one right now.
Cheers,
-Jon
It is there, but marked as undocumented.
It is listed in our version history, so I think it’s an oversight on our behalf.
The function takes a string as an argument and returnes a game object with the name in the string. The string can also contain /es to traverse the transform hierarchy, if I remember correctly. If the object does not exist, the function will return null.
So, what is the difference between FindGameObjectWithTag() and GameObject.Find()?
GameObject.Find() looks for an object by its name, and GameObject.FindGameObjectWithTag() looks for an object by its tag which are created in Edit → Project Settings → Tags. The tags are set in the Inspector of the object.
Tags are annoying to set up and they don’t transfer when you import and export packages. (An annoyance when working in teams)
So, I suppose it comes down to lazyness. Then again, isn’t that why we’re all using Unity anyways?
Cheers,
-Jon
The idea behind GameObject.Find was that you can do this:
GameObject.Find (“/Monster/Arm/Hand”);
and this:
GameObject.Find (“Monster/Arm/Hand”);
Where the first has to have a game object as root called monster with children arm/hand.
The second function call has to have a game object called monster anywhere in the hierarchy, with the children arm/hand.
At the moment unity will do the same in both cases above.
For 1.1.1 it will do what i described above, at which point we will also document it.
Sounds like the function for me
Hot diggity! I’ll have to use that on my next project.
W00t!
Now, all I need is to be able to set object names in a script, then bliss!
You actually can set object names from a script.
gameObject.name = “dudu”;
Anyway, i suggest you don’t go wild using this method in performance critical code.
Does changing the tag reduce performance as well?
Yes. The deal here is, you are probably not going to notice it if you do 5 of those per frame, but just don’t do it carelessly.
In most of the cases you will want to cache the result in a private variable anyway.
Of course for small scenes the performance of this is also very fast and totally neglectable.