Question about using FindGameObject(s)WithTag x FindObject(s)OfType x Find ?

Hi Experts Unity!

I like to learn more when use these methods in each situation they which are more desirable, are methods to find objects in game:

Find Finds a game object by name and returns it.
FindGameObjectsWithTag Returns a list of active GameObjects tagged tag. Returns empty array if no GameObject was found.
FindWithTag Returns one active GameObject tagged tag. Returns null if no GameObject was found.
FindObjectOfType Returns the first active loaded object of Type type.
FindObjectsOfType Returns a list of all active loaded objects of Type type.

I wonder what you think , and when to use :
FindWithTag x Find x FindObjectOfType
FindObjectsWithType x FindGameObjectsWithTag

Taking into account best practices of programming and performance.

Source: Unity - Scripting API: GameObject

I wish we could build together the end of the topic , a capable knowledge exemplify scenarios / situations / mechanics that would be recommended for each of the above methods !

Example: If I need to do xxxxxxxxx , the best solution would be to use the Find … why xxxxxxxxxxxxxxxx .

I count on your help!

Regards,

Deiverson
BigDreams Game Studio - coming soon!

Honestly? As much as possible, you should use none of them, especially GameObject.Find*. They are slow, and they give results that can flake out with the slightest change in your project (and not give you any errors about it until runtime). It’s been years since I’ve used GameObject.Find in any of my own projects, and I haven’t missed it. That’s not to say there are no uses for it, just…very few.

And tags suck, in general. If you need to tag an object, “tag” it with an empty MonoBehaviour - there’s no real performance hit and it’ll be 100x more useful. For example, you can stack multiple “tags” if you use MonoBehaviours, and there are no methods for transform.FindTagInChildren.

FindObjectOfType is a better alternative in almost every case, at least. It’s still pretty slow and should be used sparingly, but at least it gives you reliable, usable results right away. You KNOW that FindObjectOfType is going to return your player, whereas GameObject.Find(“Player”) can find any object in the hierarchy that you may have absent-mindedly named “Player” (for example, in the UI as a child of the “list of scores” box or something). Or, more likely, GameObject.Find(“Player”) will fail to find objects named “Player(Clone)”, when six month from now you switch to a gameplay manager that instantiates the player object after character selection. However, even with all its advantages over GameObject.Find*, I still pretty much only use it when I’m lazy or in a rush, or if I need to find all of a component I don’t have control over (e.g. the internal Unity components).

The best technique to use, if you think you will be in a position to use GameObject.Find(“Player”), consider using a singleton instead. Have your player script set a static reference to itself when it starts; you get the benefit of FindObjectOfType, except it’s fast. Similarly, it’s not hard to write a static list of all of a particular class of objects that is modified in OnEnable and OnDisable, and this is going to be faster than FindObjectOfType.

4 Likes