creating a list by finding game objects with more than tags

Greetings all,

I am trying to create a array of game objects but using more than just tags as an identifier.

I create the array currently with this line of code:

GameObject[] mates = GameObject.FindGameObjectsWithTag(mateTag);

Which is working just fine, but is there a way to “find game objects with name”?
Or “find game objects with layer”?

Follow up, if I can do that, then could I theoretically create an array with more than one requirement? Like this:

GameObject[] mates = GameObject.FindGameObjectsWithTag(mateTag) && GameObject.FindGameObjectsWithLayer(creature);

Or perhaps after finding all the game objects with a specific tag, it can search that array for all objects that meet certain parameters?

From a language standpoint you can glom a bunch of arrays together by using a list:

// starting empty to make a point
List<GameObject> mates = new List<GameObject>();

// add the mateTag ones
mates.AddRange( GameObject.FindGameObjectsWithTag( mateTag));

// add the susTag ones
mates.AddRange( GameObject.FindGameObjectsWithTag( susTag));

// and one more for my special buddy
mates.Add( SpecialFinalMateWhomYouAlreadyHaveAReferenceTo);

// and finally when you want it, either use it as the list, OR..
//  use .ToArray() on the list to get an array
1 Like

To find by name, use the ‘Find’ method:

I don’t know of a way to find by layer directly. Of course you could just find all objects of type GameObject and then filter for the layer. Something like:

var objectsOnLayer = Object.FindObjectsOfType<GameObject>().Where(o => o.layer == someLayer).ToArray();

As for having some filter that does all of these, you can join these collections in some manner. But this starts getting into a bigger issue… that being performance.

Find by tag, or Find by name, or Find by type are all notoriously slow methods as they traverse the entire state of the game. Joining them is just going to be even slower. Your best bet as the most performant is doing like my above example searching for layer, and doing all your comparisons in the linq.

var arr = Object.FindObjectsOfType<GameObject>().Where(o => o.layer == someLayer && o.CompareTag("SomeTag") && o.name == "SomeName").ToArray();

But even then… it’s slow and isn’t really preferred.

Instead… what are you trying to accomplish? Because these “find” methods are generally not the way you want to go about it. They mainly exist as last ditch options and/or because they’ve existed since early versions of unity back when unity made some design choices that aren’t really intended to be used these days.

1 Like

I never run the functions in a standard update so I have not had any issues with slowdown yet.

The goal is I am trying to have one object look at a object in a range and check if it meets certain requirements.

That is the most simple way of saying it.

Unfortunately combining the arrays isn’t an option, since this would combine all objects that fit one requirement or the other, and not necessarily ones that meet both.