Search Unity

  1. Unity 2020.1 has been released.
    Dismiss Notice
  2. We are looking for feedback on the experimental Unity Safe Mode which is aiming to help you resolve compilation errors faster during project startup.
    Dismiss Notice
  3. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Which is better in performance, finding an object vs. passing an object as a parameter.

Discussion in 'Editor & General Support' started by Davidolee, Jun 25, 2020.

  1. Davidolee

    Davidolee

    Joined:
    May 8, 2016
    Posts:
    2
    Let's say there is a method that is finding an object. It also can find the object with a parameter.
    If the method is called very frequently, which one should i take?
    Thank in advance for any help :)
     
  2. jamespaterson

    jamespaterson

    Joined:
    Jun 19, 2018
    Posts:
    239
    If you are asking which is faster, to use gameobject.Find("name of object") or passing the game object as a parameter of a function e.g. void dostuff(gameobject obj) then the answer is passing by function parameter should be faster most of the time. Probably all of the time.
     
    Joe-Censored likes this.
  3. Davidolee

    Davidolee

    Joined:
    May 8, 2016
    Posts:
    2
    @jamespasterson Thank you so much!! That's what I want to know. :)
     
    jamespaterson likes this.
  4. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    3,200
    I pretty much never use GameObject.Find unless it's during scene setup while I still have a loading screen showing. Note from the documentation of GameObject.Find:

    So, pretty much any of the GameObject.Find*** methods are bad for performance. You can get away with using them a bit, but you don't want to rely on them too heavily. But it depends on your game. If your game isn't doing too much else, you might never notice the performance of GameObject.Find.
     
  5. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    8,759
    Generally if you are deciding between what is faster, GameObject.Find or <insert literally anything else>, the answer won't be GameObject.Find.
     
    PraetorBlue and Vryken like this.
unityunity