Search Unity

Find

Discussion in 'Scripting' started by Jonathan Czeck, Sep 13, 2005.

  1. Jonathan Czeck

    Jonathan Czeck

    Joined:
    Mar 17, 2005
    Posts:
    1,713
    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
     
  2. hsparra

    hsparra

    Joined:
    Jul 12, 2005
    Posts:
    750
  3. Jonathan Czeck

    Jonathan Czeck

    Joined:
    Mar 17, 2005
    Posts:
    1,713
    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
     
  4. freyr

    freyr

    Joined:
    Apr 7, 2005
    Posts:
    1,148
    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.
     
  5. hsparra

    hsparra

    Joined:
    Jul 12, 2005
    Posts:
    750
    So, what is the difference between FindGameObjectWithTag() and GameObject.Find()?
     
  6. Jonathan Czeck

    Jonathan Czeck

    Joined:
    Mar 17, 2005
    Posts:
    1,713
    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
     
  7. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    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.
     
  8. hsparra

    hsparra

    Joined:
    Jul 12, 2005
    Posts:
    750
    Sounds like the function for me :)
     
  9. Krispy

    Krispy

    Joined:
    Jun 6, 2005
    Posts:
    69
    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!
     
  10. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    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.
     
  11. klindeman

    klindeman

    Joined:
    Jun 13, 2005
    Posts:
    295
    Does changing the tag reduce performance as well?
     
  12. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    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.