Search code examples
c#unity-game-enginegameobject

Find GameObjects with keyword Unity


How can I find GameObjects with a keyword in Unity ?

Actually, I have a lot of GameObject named like that :

  • Concrete Floor [289483]
  • Concrete Floor [289487]
  • Wall part [293291]
  • Part [321043]
  • ...

I already made functions to get the ID of an object (inside the [] brackets) because it is the relevant part of the GameObject name.

ID should be unique, but I'm not sure it is !

Now, I would like to have a function with this prototype :

public static GameObject[] getObjectsWithIdentifier(int identifier)

It would return all of the objects that have the identifier given in parameter. So it's like a search function.

GameObject.Find(name), as I understand it, only make the search for the exact name of the object.

Thanks for your help !


Solution

  • Finally, this is the solution I came up with.

    This is what I made, of course it iterate, but I think I will create the initial global array only once at the first time you use the function.

    public static Object[] findObjectsFromIdentifier(int identifier) {
        Object[] objects = GameObject.FindObjectsOfType( typeof( GameObject ) );
        return objects.Where( obj => getIdentifierFromObject( obj ) == identifier ).ToArray();
    }