Finding and collecting GameObjects

Syntax

  • public static GameObject Find(string name);
  • public static GameObject FindGameObjectWithTag(string tag);
  • public static GameObject[] FindGameObjectsWithTag(string tag);
  • public static Object FindObjectOfType(Type type);
  • public static Object[] FindObjectsOfType(Type type);

Remarks

Which method to use

Be careful while looking for GameObjects at runtime, as this can be resource consuming. Especially : don't run FindObjectOfType or Find in Update, FixedUpdate or more generally in a method called one or more time per frame.

  • Call runtime methods FindObjectOfType and Find only when necessary
  • FindGameObjectWithTag has very good performance compared to other string based methods. Unity keeps separate tabs on tagged objects and queries those instead of the entire scene.
  • For "static" GameObjects (such as UI elements and prefabs) created in the editor use serializable GameObject reference in the editor
  • Keep your lists of GameObjects in List or Arrays that you manage yourself
  • In general, if you instantiate a lot of GameObjects of the same type take a look at Object Pooling
  • Cache your search results to avoid running the expensive search methods again and again.

Going deeper

Besides the methods that come with Unity, it's relatively easy to design your own search and collection methods.

  • In case of FindObjectsOfType(), you could have your scripts keep a list of themselves in a static collection. It is far faster to iterate a ready list of objects than to search and inspect objects from the scene.

  • Or make a script that stores their instances in a string based Dictionary, and you have a simple tagging system you can expand upon.

Find GameObjects by name from child objects

Transform tr = GetComponent<Transform>().Find("NameOfTheObject");
GameObject go = tr.gameObject;

Find returns null if none is found

ProsCons
Limited, well defined search scopeStrings are weak references

Finding GameObjects by MonoBehaviour scripts

ExampleScript script = GameObject.FindObjectOfType<ExampleScript>();
GameObject go = script.gameObject;

FindObjectOfType() returns null if none is found.

ProsCons
Strongly typedPerformance degrades along the number of gameobjects needed to evaluate
Possible to search both single objects and entire groups

Inserted to scripts in Edit Mode

[SerializeField]
GameObject[] gameObjects;
ProsCons
Great performanceObject collection is static
Portable codeCan only refer to GameObjects from the same scene

Searching by GameObject's name

var go = GameObject.Find("NameOfTheObject");
ProsCons
Easy to usePerformance degrades along the number of gameobjects in scene
Strings are weak references and suspect to user errors

Searching by GameObject's tags

var go = GameObject.FindGameObjectWithTag("Player");
ProsCons
Possible to search both single objects and entire groupsStrings are weak references and suspect to user errors.
Relatively fast and efficientCode is not portable as tags are hard coded in scripts.