How do I find all game objects with the same name?

The GameObject.Find method returns a single object, but Unity allows you to give multiple objects the same name. How can I find all instances of an object that have the same name?

There are several ways to find multiple objects.

By Tag (recommended)

FindGameObjectsWithTag will find game objects with the specified tag (not name), meaning all of the objects you are looking for would have to have the same tag. This returns an array of GameObjects which you can iterate through to check the name if you need to use the name and cannot simply use tags.

Unity js

for(var fooObj : GameObject in GameObject.FindGameObjectsWithTag("foo"))
{
    if(fooObj.name == "bar")
    {
        //Do something...
    }
}

Unity mono

foreach(GameObject fooObj in GameObject.FindGameObjectsWithTag("foo"))
{
    if(fooObj.name == "bar")
    {
        //Do Something
    }
}

By Type

FindObjectsOfType will find all objects of a certain type, meaning that all of these objects will need to have the same type. This returns an array of Objects which you can treat as the type you searched for.

Unity js

//You probably want a more specific type than GameObject
for(var gameObj : GameObject in GameObject.FindObjectsOfType(GameObject))
{
    if(gameObj.name == "bar")
    {
        //Do something...
    }
}

C#

//You probably want a more specific type than GameObject
foreach (var gameObj in FindObjectsOfType(typeof(GameObject)) as GameObject[])
{
    if(gameObj.name == "bar")
    {
        //Do something...
    }
}

Find all Objects With Name:

var objects = Resources.FindObjectsOfTypeAll<GameObject>().Where(obj => obj.name == "Name");

This returns a list called “Objects” containing all active and inactive game objects with a name matching the provided string.

Note: This code requires LINQ Queries.

using System.Linq;

Four years later…
HEY GUYS I FOUND A WAY… silence…

Now, seriously, here is my way of finding all objects with the same name. Since there is no direct way of doing it the code is quite big:

here is my whole in-game console script (not finished):

var display : boolean;
var actionIndex : int;
var AINA : String[] = ["Level", "Object", "Variable", "Command", "PlayerPrefs"];
var objectName : String;
var objectIndex : int;
var checkIndex : int;
var foundObjects : GameObject[];
var variableName : String;
var command : String;
var playerPrefsName : String;
var playerPrefsInt : int;
var playerPrefsFloat : float;
var playerPrefsString : String;
var levelToLoad : String;
var TCS : Component[]; //temporary component store
var TCSI : int; //temporary component store index

function Start () {
	TCS = new Component[1];
}

function Awake () {
	DontDestroyOnLoad(gameObject);
}

function Update () {
	if(Input.GetKey(KeyCode.LeftControl))
	{
		if(Input.GetKeyDown(KeyCode.F1))
		{
			if(!display)
			{
				display = true;
			}
			else
			{
				display = false;
			}
		}
	}
}

function OnGUI () {
	if(display)
	{
		GUILayout.BeginArea(Rect(0,0,512,250),"","Box");
		GUILayout.BeginArea(Rect(5,5,502,35));
		actionIndex = GUILayout.SelectionGrid(actionIndex,AINA,AINA.Length,GUILayout.Height(35));
		GUILayout.EndArea();
		
		if(actionIndex == 0)
		{
			levelToLoad = GUI.TextField(Rect(5,50,502,25),levelToLoad);
			if(GUI.Button(Rect(5,80,502,25),"Load"))
			{
				Application.LoadLevel(levelToLoad);
			}
		}
		
		if(actionIndex == 1)
		{
			objectName = GUI.TextField(Rect(5,50,302,25),objectName);
			if(GUI.Button(Rect(312,50,95,25), "By tag"))
			{
				FindObjectsWithTag();
			}
			if(GUI.Button(Rect(412,50,95,25), "By name"))
			{
				FindAllObjects();
			}
		}
		
		
		GUILayout.EndArea();
	}
}

function FindObjectsWithTag () {
	var objects = GameObject.FindGameObjectsWithTag(objectName);
	foundObjects = new GameObject[objects.Length];
	foundObjects = objects;
}

function FindAllObjects () {
	var objectsn = GameObject.FindObjectsOfType(GameObject);
	for(var f = 0; f < objectsn.Length; f++)
	{
		if(objectsn[f].name != objectName)
		{
			objectsn[f] = null;
		}
	}
	for(var i = 0; i < objectsn.Length; i++)
	{
		if(objectsn *!= null)*
  •  {*
    
  •  	checkIndex ++;*
    
  •  }*
    
  • }*
  • foundObjects = new GameObject[checkIndex];*
  • checkIndex = 0;*
  • for(var a = 0; a < objectsn.Length; a++ )*
  • {*
  •  if(objectsn[a] != null)*
    
  •  {*
    
  •  	foundObjects[checkIndex] = objectsn[a];*
    
  •  	checkIndex++;*
    
  •  }*
    
  • }*
  • checkIndex = 0;*

}

function ResizeTCS () {

  • TCS = new Component[TCS.Length + 1];*
    }
    Now, the important part:
    var objectName : String;
    var checkIndex : int;
    var foundObjects : GameObject[];
    function FindAllObjects () {
  • var objectsn = GameObject.FindObjectsOfType(GameObject); //get all objects*
  • for(var f = 0; f < objectsn.Length; f++) //filter the objects that don’t match*
  • {*
  •  if(objectsn[f].name != objectName)*
    
  •  {*
    
  •  	objectsn[f] = null;*
    
  •  }*
    
  • }*
  • for(var i = 0; i < objectsn.Length; i++) //check how may objects are left in there*
  • {*
    _ if(objectsn != null)_
    * {*
    * checkIndex ++;*
    * }*
    * }*
    * foundObjects = new GameObject[checkIndex]; //resize*
    * checkIndex = 0;*
    * for(var a = 0; a < objectsn.Length; a++ ) //apply the objects to a new array*
    * {*
    * if(objectsn[a] != null)*
    * {*
    * foundObjects[checkIndex] = objectsn[a];*
    * checkIndex++;*
    * }*
    * }*
    * checkIndex = 0;*

}

They don't provide this functionality for you because `Find()` is not very performant, even for one object (let alone returning an array of objects). You're also probably not supposed to be naming similar objects the exact same if you want to group them. Instead, you should use a tag, and `GameObject.FindGameObjectsWithTag()` which does return an array of game objects with that particular tag (not to mention it's faster, and the correct way to "group" game objects).

If you really want to work with the names, this c# void gives the array of all GameObjects with the specific name.

GameObject[] FindGameObjectsWithName(string name){
		int a = GameObject.FindObjectsOfType <GameObject>().Length;
		GameObject[] arr=new GameObject[a];
		int FluentNumber = 0;
		for (int i=0; i<a; i++) {
			if (GameObject.FindObjectsOfType<GameObject> () *.name == name) {*

_ arr [FluentNumber] = GameObject.FindObjectsOfType () ;_
* FluentNumber++;*
* }*
* }*
* Array.Resize (ref arr, FluentNumber);*
* return arr;*
* }*

I would suggest making 6 classes to solve this problem. SceneX, SceneMangerX, TransformX, GameObjectX, TransformArrayX, and CanvasGameObjectFinder(this is just to test it by using the gear button on the inspector of the script attached to a gameobject in one of the scenes)

This approach is indifferent to whether or not the GameObject is active in the scene or not.

using UnityEngine;
using UnityEngine.SceneManagement;
using System.Collections;
using System.Collections.Generic;
using System;
public static class SceneX 
{
	public static GameObject[] GetAllGameObjects(this Scene me)
    {
        GameObject[] rootGameObjects = me.GetRootGameObjects();
        List<GameObject> allGameObjects = new List<GameObject>();
        allGameObjects.AddRange(rootGameObjects);
        for (int i = 0; i < rootGameObjects.Length; i++)
        {
            allGameObjects.AddRange(rootGameObjects*.GetKids(true));*

}
return allGameObjects.ToArray();
}
}

using UnityEngine;
using UnityEngine.SceneManagement;
using System.Collections;
using System.Collections.Generic;
public static class SceneManagerX
{

  • public static Scene GetAllLoadedScenes()*
    {
    Scene[] scenes = new Scene[SceneManager.sceneCount];
    for (int i = 0; i < scenes.Length; i++)
    {
    scenes = SceneManager.GetSceneAt(i);
    }
    return scenes;
    }

* public static GameObject[] GetAllLoadedSceneGameObjects()*
* {*
* Scene[] scenes = SceneManagerX.GetAllLoadedScenes();*
* List gameObjects = new List();*
* for (int i = 0; i < scenes.Length; i++)*
* {*
_ gameObjects.AddRange(scenes*.GetAllGameObjects());
}
return gameObjects.ToArray();
}*_

* public static GameObject[] GetAllLoadedSceneGameObjectsWithName(string name)*
* {*
* GameObject[] gameObjects = GetAllLoadedSceneGameObjects();*
* List matchedGameObjects = new List();*
* for (int i = 0; i < gameObjects.Length; i++)*
* {*
_ if(gameObjects*.name == name)
{
matchedGameObjects.Add(gameObjects);
}
}
return matchedGameObjects.ToArray();
}
}*_

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
public static class TransformX
{
* public static Transform[] GetKids(this Transform me)*
{
List kids = new List();
foreach (Transform kid in me)
{
kids.Add(kid);
}
return kids.ToArray();
}

public static Transform[] GetKids(this Transform me, bool isRecursive)
{
if (!isRecursive)
return me.GetKids();
List kids = new List();
foreach (Transform kid in me)
{
kids.Add(kid);
}
return GetKidsRecursive(me);
}

static Transform[] GetKidsRecursive(Transform kid)
{
Transform[] newKids = kid.GetKids();
List AllKids = new List();
AllKids.AddRange(newKids);
for (int i = 0; i < newKids.Length; i++)
{
AllKids.AddRange(GetKidsRecursive(newKids*));*
}
return AllKids.ToArray();
}
}

using UnityEngine;
using System.Collections.Generic;
public static class GameObjectX
{
* public static GameObject[] GetKids(this GameObject me, bool isRecursive = false)*
{
Transform[] kids = me.transform.GetKids(isRecursive);
return kids.ToGameObjectArray();
}
}

using UnityEngine;
using System;
using System.Collections.Generic;
public static class TransformArrayX
{
public static GameObject[] ToGameObjectArray(this Transform[] me)
{
GameObject[] gos = new GameObject[me.Length];
for (int i = 0; i < me.Length; i++)
{
gos = me*.gameObject;*
}
return gos;
}
}

using UnityEngine;
using UnityEngine.SceneManagement;
using System;
public class CanvasGameObjectFinder : MonoBehaviour
{
* [ContextMenu(“Find All Canvas GameObjects”)]*
private void FindCanvasGameObjectsContext()
{
GameObject[] gameObjects = SceneManagerX.GetAllLoadedSceneGameObjectsWithName(“Canvas”);
for (int i = 0; i < gameObjects.Length; i++)
{
Debug.Log(gameObjects*);*
}
}
}

Jeez guys. wish i would have seen this question years ago. All you have to do is think outside the box a bit . here is what i wrote. this works magically for all versions old and new using only Gameobject.Find.

int i = 0;
            public GameObject[] friends;
		bool b = false;
		while (!b) {
            if(GameObject.Find("friend")){
				GameObject.Find("friend").transform.name="friend"+i;
				i++;
			}else{b=true; }}

		friends = new GameObject*;*
  •  while (i>0) {i--;*
    

friends*=GameObject.Find(“friend”+i);*
_ friends*.transform.name=“friend”;
}*_