- Home /
Best way to determine if there is a gameobject in multiple locations?
Hi all. How do I determine if there are no gameobjects in set locations?
I'm making a 2d tile based game and I want to check if there are any gameobjects in a set coordinate. I have no problems checking if there are any gameobjects in a location. I just ran a foreach for every gameobject with certain tags and do actions if they are in certain tiles. However, I don't know what to do for empty tiles.
I'm planning to make an empty gameobject with a collider then iterate it through the locations and check if there are any trigger/collision to other gameobjects. Should I got through this method or are there any simpler way to do this? Thank you in advance.
I dont understand ... why dont you run foreach for every gameobject with certain tags as you wrote ?
I have a 2d array that shows the content of each tile. It should have a value of 2 if there is a player character, 1 if there is a wall, etc.
Foreach would work if I have a gameobject in every location/tile but I don't. I want to know which locations has no gameobjects so I can set the values in the array.
After reading your comment I just thought of putting empty gameobjects with the tag "empty" on locations with no players, enemies, walls, etc. Would it be easier if I would make gameobjects with the tag "empty" for empty tiles rather than checking if there any gameobjects in set locations/tiles?
why dont you intialize array with 0, and then run foreach for every object and change number of every tile where something is? So empty tiles have value 0.
Answer by Andres-Fernandez · Jul 07, 2014 at 12:10 PM
Well, I can think of a couple of solutions.
First and easiest: just have a logical representation of your map. Being a 2d array, text file, whatever you want. Make your objects update your logical map whenever they move/change position and then you only need to check your logical map at the desired position.
Second: Create an array of tile positions on top of the tiles and send raycasts from each tile position to the tile. If collides with any object, then the tile is occupied. Else, it's free.
I think your first solution isnt the easiest, but it is the best certainly. Sending raycast, creating empty objects or even triggers is not good.
Well, first one seems the easiest to me. I would go for the 2D array (tile based games are perfect for that kind of logical representation).
For the second one you don't need to create anything, just have a Vector3 array to send the raycasts from and one layer to check for the characters. No triggers or anything. If your characters have colliders on, just send the raycast. Although it would cost a lot of time, that's for sure.
yes, the first one is most effective, so it is the best for me. But you need to change the array every time when anything moves, so you can do some bugs easily. That is why I dont think it is most easy (something is easy for me if it is easy to code, but I like doing things effective)
Your answer
![](https://koobas.hobune.stream/wayback/20220613153433im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Spawning GameObjects with help of classes --- Attaching classes to GameObjects 1 Answer
Instantiating Multiple Game Objects to the Position of a Current GameObject 2 Answers
How to apply more than one trigger on a single gamebject 1 Answer
using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers
Please help. How do I add a 3D Object at a specific location in the GO Maps package? 0 Answers