Hello everyone I have been trying to solve this problem for couple of days but with no luck.
Here is a setup. Game that I am doing is a match 3 type game. Board has Gems that are GameObjects and they all have Box Collider. Every gem has a tag “Gem” and every gem has 4 Detectors. One on tope bottom left and right. They also have Box Collider and a Script. This is the code of the Detectors Script.
public Gem owner;
void OnTriggerEnter(Collider c)
{
if(c.tag == "Gem")
{
owner.AddNeighbor(c.GetComponent<Gem>());
}
}
void OnTriggerExit(Collider c)
{
if(c.tag == "Gem")
{
owner.RemoveNeighbor(c.GetComponent<Gem>());
}
}
And a code from Gem script
public void AddNeighbor(Gem g)
{
if(!Neighbors.Contains(g))
{
Neighbors.Add(g);
}
}
public void RemoveNeighbor(Gem g)
{
Neighbors.Remove(g);
}
And now the problem.
When I start moving and matching gems some of the gems do not get their neighbors right. For example I match first 3 gems on Y axes and they are destroyed from top come falling 3 new gems. Gem that falls on the 4th gem on Y axes does not add the 4th gem as his neighbor and the 4th gem does not add 3rd gem as his neighbor. The other problem is that when I move gem sometimes he does not remove his old neighbor but just add new one then I have some weird matches because I am checking through the neighbor list to see potential matches. When the Board is created all gems have there neighbors properly detected.