Is there a way to use a Regular expression in the GameObject.Find() method?

I'm making a checkers game and I've got pieces named Red Piece (x,y) and White Piece (x,y) (where x & y are actual integer coordinates). I want to destroy a piece at a certain x,y. So what I'm trying to do is something like

Destroy(GameObject.Find(Regex.Matches("(Red)|(White) Piece (" + x + ","+ y + ")")));

I need it to find any object with a name that contains "Piece (x,y)" whether it's a red piece or a white piece, and destroy it. Any ideas?

  • 3
    \$\begingroup\$ What are you actually trying to do? This seems like an XY problem. \$\endgroup\$
    – user1430
    May 10, 2016 at 20:46
  • \$\begingroup\$ I suggest you make a minigame by following one of the great tutorials available. This will drastically help you in much less time than trying to learn the basics on your own. \$\endgroup\$
    – Evorlor
    May 10, 2016 at 21:54

2 Answers 2


No, not using GameObject.Find.

You could, maintain your own list of object names, and search that using regex. Then pass the found string into GameObject.Find to get the actual game object from the name.

However, I'm not really clear on why you're naming checkers pieces to include their location (since checkers pieces move). But you can find game objects at a specific location, using something like a raycast. But I'd recommend simply reorganizing the way you manage objects. There are a few ways to do that. Maintain a 2D array that contains references to all the gameobjects for the pieces. Or keep better information about how you're moving pieces, to include the spaces they're "jumping".


Find cannot do this. You could try using FindObjectsOfType to get an array of GameObjects, and then manually filter that list by rejecting any with a name that doesn't match your pattern.

However, it seems like you could be solving the issue at a higher level. For example, if you're just trying to remove all "piece" game objects from the world, leaving behind any other game objects you're using for other things, you could consider:

  • tagging each of those objects when you create them (or otherwise perform the manipulation on them that gets them ready to be deleted), and then using FindObjectsWithTag

  • similarly, put them into a List<GameObject> called pieces (or something) so that you can find them all later to destroy them.

The last option is potentially far, far more efficient than having to traverse every game object in the world and perform a regex, or even just a substring comparison, against them.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .