- Home /
How to detect Neighbours?
Hi there!
i have a grid (2D array) of objects and I want a gameobject to detect its neighbours when clicked and let them ask, if neighbour is of same kind (ID). I also want to know if the neighbours neighbour also is of same kind. so I thought best would be to start a cascade and let every neighhbour checks its own left/right/top/bottom neighbours and then let them tell their neighbours to check theirs...
works fine. but now I need to know, when the cascade is finished. so who is telling me about that. hm...?
:-)
thnx!
Answer by Bampf · Mar 24, 2011 at 03:36 PM
The exact implementation depends on what you are doing with the result, and on what your code looks like so far. But the basic idea would be to keep track of whether a particular cell had already been checked. Some ways to do that:
- keep a list of coordinates that have been visited
- keep a list of GameObjects that have been checked already
- keep 2D array of booleans of the same size as the grid, and set each cell to true when it had been visited
- each GameObject could keep the time that it was last checked. When a neighbor asks it, it can see if it already answered the question this frame. If the current time and the stored time are different, then this is the first time its been asked during this frame.
Your answer
![](https://koobas.hobune.stream/wayback/20220613114624im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Detect nearest neighbours (and their neighbours...) 2 Answers
OnCollisionEnter can't find 'Player'? 3 Answers
Seven key press 3 Answers