Which GameObject.Find() returns if there are more than one object have the same name?

GameObject class has some functions such as GameObject.Find, GameObject.FindWithTag which return only 1 GameObject. If there are many Game Objects with the same name, or same tag, which is the result of those functions?
Thanks.

You can try
GameObject.FindGameObjectsWithTag(); It will find list of gameobjects with the tag and return GameObject[ ].

Or
Object.FindObjectsOfType();But i won’t recommend the latter meaning the find by type method because it is very slow according to the documentation and so use the first one. :smile:

Thank for your reply.
I also saw GameObject.FindGameObjectsWithTag and GameObject.FindObjectsOfType, but still want to know how GameObject.Find and GameObject.FindWithTag work. If there are many objects have the same tag in my scene, which object is returned?

Try it out for yourself, and tell us about it. :wink:

GameObject.Find return 1 object while GameObject.FindGameObjectsWithTag return multiple objects.

Whichever one it comes across first. If you’re after a specific object, you shouldn’t rely on this behavior.

–Eric

3 Likes
GameObject.Find("name"); // returns a single object, or null
GameObject.FindGameObjectWithTag("tagName");//returns (randomly, i think) one of the objects that have the required tag
GameObject.FindGameObjectsWithTag("tagName");//returns a list of gameobjects that have the required tag

I do not know which object is the one it returns, but if you have to choose one of many, use raycast, collisions, use distance as a sorting criteria or something similar.

Thanks for your support

I tried, but still didn’t understand clearly.

Yes, I know know it returns 1 object, but I don’t know which object is returned in multiple objects have the same name or tag?

The problem is I don’t know which one comes across first? Is it a random object?

Yes, I also must use some additional properties to determine action for that object.

I would suspect it picks out the same object each time. Which one depends on how Unity stores its information.

The problem isn’t that you don’t know which one comes first, because you should not be using this method when you require specific objects.

It’s undefined. So you should only use GameObject.Find when you have one object with that name, or if you don’t care which one is picked.

–Eric

1 Like

I am thinking if you have 2 objects on the scene already when the game starts, you need not use gameobject.find. You can just use a reference which is faster. However if your objects is instantiate during runtime, that wouldn’t work. So just a suggestion.

It would be easier to recommend usage if we knew why you are fetching an object reference. One of the reasons I added numbers to the ends of instance names in PoolManager is because it helped debugging by avoiding objects with the same name. Unity is the only 3D package I have ever used that allows two objects of the same name (and I’ve used them all, including 2 proprietary packages).

Anyway, please explain, even roughly, what you are trying to do, and I’ll be able to offer some insight.

I create a game scene, which has many objects generated at runtime. They have same tag and same name like: Prefab name + “(Clone)”.
For example, I want to find the 3rd object (order by generated time), can I do this with Find and FindWithTag? I’m using FindGameObjectsWithTag and some other properties to specify game object, it’s required more resources, I want another cheaper method.

I can’t find PoolManager in Unity, can you please show me? Thanks much.

You can keep a generic list as you create the objects and then get them from the list. If you add instances to a list, you can be sure of the order.

It should appear in the store any day now, but you can get it from our website, where you can also see all the features and a video demo. http://poolmanager.path-o-logical.com (or click the link below in my signature)

Gonna bump this because relevant.
I have 2 gameobejcts with the same “Name”. Is there some unique ID for gameobject that can get me 2nd “Name” go?

Or do I have to, jesus:

iterate through all GOs in scene
find these that match Name
then iterate through their components and/or children to find something unique that help me distinguish the one I need

GameObject.Find does exactly what you’re describing - it looks through all of the objects in the scene, one by one, until it finds the correct object. It’s really fuggin slow. Don’t actually use it!

I did some checks, and if the object with the name is on the bottom of the hierarchy, it takes significantly longer to find than if it’s on the top.

If you need to uniquely identify some object, give it a script so it can register itself, or solve the problem in some other way.

I have the same question today, I thought it was the names’ alphabeta order, it’s not.

if you have multiple objects with same tag, it seems GameObject.FindGameObjectWithTag only go find the objects you load in hierarchy most recently. Once it find it, it ignore the rest.

There is no guarantee about ordering FindWithTag queries.
If you need all the active game objects with the tag try GameObject.FindGameObjectsWithTag.

1 Like

yes, and this is even in the documentation “Note: This method returns the first GameObject it finds with the specified tag. If a scene contains multiple active GameObjects with the specified tag, there is no guarantee this method will return a specific GameObject”

the question is rather why would somebody want to use it this way when you can
-make a collection of your objects when you create them (list array directory etc)
-you can give them individual names after creation
-you could find all of them with the FindGameObjectsWithTag Ninja have already mentioned
-attach them to an empty gameobject and just cycle through its childs whenever you need a specific one
-give them a name or id in their script and look for that instead of the object itself

there are many ways of doing it so why trying an unreliable way?

My guess is GameObjects are referenced in some internal undocumented collection, and Unity just iterates the collection until it finds the first one with that tag.