Search Unity

GameObject.Find

Discussion in 'Scripting' started by All_American, Jan 20, 2020.

  1. All_American

    All_American

    Joined:
    Oct 14, 2011
    Posts:
    1,528
    Why is GameObject.Find not good to use?

    Is the FindwithTag or tags any better? Is it because there’s not as many tags as there are GameObjects?

    Or is there a better way?
     
  2. ZO5KmUG6R

    ZO5KmUG6R

    Joined:
    Jul 15, 2010
    Posts:
    490
    It's slow and it searches the entire scene for a GameObject with that name, same goes with finding tags.

    The better way is referencing it directly with a variable.
    Code (CSharp):
    1. [SerializeField]
    2. private GameObject theObject;
    and assigning it in the insepector.
     
  3. Stephen_O

    Stephen_O

    Joined:
    Jun 8, 2013
    Posts:
    1,510
    It's ok to use, just not every frame since it loops through all game objects in scene for lookup. If you use it at awake/start to cache a reference that's likely ok, just know excessive use can impact scene startup time. Using it in update is bad. I believe the FindwithTag only loops through the objects with said tag so is a little faster, but still not good for update.

    The thing to do, if you need this in update is to cache the reference so you don't need to iterate through objects. This also helps reduce garbage collection associated with looking up objects.
     
    Emolk and All_American like this.
  4. All_American

    All_American

    Joined:
    Oct 14, 2011
    Posts:
    1,528
    What if you are downloading models at runtime and can't do that?
     
  5. All_American

    All_American

    Joined:
    Oct 14, 2011
    Posts:
    1,528

    I don't put it in Update, but I do use it.
     
  6. Stephen_O

    Stephen_O

    Joined:
    Jun 8, 2013
    Posts:
    1,510
    You're probably ok then, since it sounds like you only do it for one frame. Unless this is done during gameplay and not only at scene start, which could cause a performance spike if your scene is large or if you're on low end hardware.

    If you want to get rid of FindObject completely, one option is to have your dynamically loaded objects register to a static instance of a manager class (singleton) on their awake/start call which would happen when they are instantiated. Create a register object method in your manager class that can receive the call from a registerObject script on your object and have the register method setup what you need. Using this logic will allow you to configure your scene references in the manager and give the dynamically loaded objects the option to register with manager to use those references without the more expensive FindObject.
     
    All_American likes this.
  7. All_American

    All_American

    Joined:
    Oct 14, 2011
    Posts:
    1,528
    That is exactly what I am thinking about doing now. Running everything from a class. right now I am in all Mono besides the one class I have to serialize everything.

    Thank you Stephen.
     
    Stephen_O likes this.