Is GameObject.FindObjectOfType() less expensive than GameObject.Find()?

Hello,

I’m on my last finishing touches with my indie game and I’ve been thinking about improving my game’s performance. I was wondering if GameObject.Find() is less expensive than GameObject.FindObjectOfType()? Thanks!

In general this cannot be answered i think.

It will heavily depend on the amount of GameObjects you have in your scene. The more objects there are, the worse these function will perform. However the key here is where you use them.

Take a look at these 2 examples:

//Number1:
    public GameObject someObject;

    public void Start()
    {
        someObject = GameObject.Find("someObjectName");
    }

    public void Update()
    {
        someObject.doSomething();
    }

and

//Number2:
    public void Update()
    {
        GameObject.Find("someObjectName").doSomething();
    }

Both of these do the same, but one is a good use of GameObject.Find() while the other is horrible.

Spoiler: Number2 is the one you want to avoid at all cost. Whenever you use any Find method (e.g. FindObjectOfType, Find, FindObjectWithTag) you should never ever ever use them somewhere related to an Update. Always just do this once and keep a reference. Just use them at gamestart, not during the runtime of the actual game.

In that context it is then fine to use these functions and then it is more of a convenience if you use the one or the other. These fractions of a millisecond to find once gameobject will not matter than much.
(Assuming that you only do this at gameStart)

In case that this is already the case for you then try not to blindly optimize things. Take a look at the Unity Profiler. It will tell you what in your game needs how much performance. Optimize the largest things first, do not get lost in micro-optimizations - it is not worth it, trust me.

let me know if that helped. In case you have more questions regarding this or are uncertain please let me know and share some example code of where you are using this. If we can actually see the code you use we can give you better advice on what to change.

Alright, thank you so much for the answer!