Searching a List of GameObjects by name

So I have a list of gameObjects, how would I efficiently get the gameObject with a name?

For example

List<GameObject> list = new <GameObject>();
GameObject temp = list.Find("Sword");

Easily done using Linq:

  using System.Linq;
  GameObject temp = list.Where(obj => obj.name == "Sword").SingleOrDefault();

This will return your game object if found. Otherwise, null. You have to import System.Linq though.

1 Like

Just use loop:

for(int i=0; i<list.Count; i++){
    if(list*.name == "Sword") {*

temp = list*;*
break;
}
}
can’t think of a more efficient way.