Find positions of all objects with a specified prefix

I would like to find all objects of a certain type either by prefix or category and store those values in an array. This would be used for simple grid movement checks. I’m also a bit confused by when that script should run. Should it run in the beginning of the level, and then have my player code access it as it goes?

GameObject.FindGameObjectsWithTag will return an array of GameObjects with the specified tag.

GameObject.FindGameObjectsWithTag

As to when to run it… that’s hard to nail down. I’d run it at the start and anytime I thought the amount of objects might change so if an object is destroyed or created this routine might need to run again. It really depends on what you’re trying to do in your game.

You can use,

private GameObject[] enemies;

void Start(){
enemies = GameObject.FindGameObjectsWithTag("Enemy");
}

This will put all the gameobjects with the tag enemy into that list.

Excellent, thanks for the quick reply guys! The FindGameObjectsWithTag is perfect for what I want.

To give more info on what I’m doing: I’m pretty much trying to make a puzzle demo with the mechanics of the ice mazes from the pokemon series. I wrote a little prototype with python in maya that basically looks at all the block positions I have and stores them in a list. Then with up down left right I try to move the player waypoint to the furthest available spot. This waypoint will move instantly and the player will follow it at a certain speed.

Again I definitely appreciate the help.