Finding GameObject without using the full name

Hi, I have problem with this one:

GameObject.Find("MyGameObject")

finds only GameObjects with this specific name. I have to find GameObjects with with a name that contains a a fragment of string, for example:

GameObject.Find("Car")

finds Car1, Car2 etc.
Any ideas? :slight_smile:

If you are not using your tags for anything else, then the easiest solution would be to tag all of your cars with a β€˜Car’ tag. Then you would not need a string search. You could then use GameObject.FindGameObjectsWithTag() to get all the cars. If you really need the substring search, you could use GameObject.FindObjectsOfType() to get a list of all the game objects. Then you could cycle through the list using any string comparison that makes sense to find your objects. Note that both FindGameObjectsWithTag() and FindObjectsOfType() only return active game objects and that FindObjectsOfType() is slow.

As a way of creating a more compact search if you need to cycle through all the game objects, take a look at LinQ:

http://unitygems.com/linq-1-time-linq/