How to get neighbor from for loop in hexagonal grid ?

Hello, I have a small problem, I do not know how to asign neighbours to an object.


What I want is to know which hexagons are neighbours of hexagon A.

I know that it can be done through for loop, but I do not know how to create it so that object A knows that object B,C,D,E,F,G are neighbours of object A.

Here is my script for the hexagon map generation, hope it helps

public class MapGenerator : MonoBehaviour
{
    public int mapSize;

    public GameObject hexagonCellPrefab;
    public GameObject grassLand;
    public GameObject[] hexagonCells;
    public Vector3[] cellPositions;

    public int numberOfIslands;

    public bool worldIsGenerated;
    int i;

    Vector3 hexagonPosition;
    void Start()
    {
        GenerateMap();
    }

    void GenerateMap() 
    {
        hexagonCells = new GameObject[mapSize * mapSize];
        cellPositions = new Vector3[mapSize * mapSize];

        for (int x = 0,i = 0; x < mapSize; x++)
        {
            for (int z = 0; z < mapSize; z++)
            {
                CreateHexagonCell(x, z, i++);
                worldIsGenerated = true;            
            }
        }

        /* 
        if (worldIsGenerated)
        {
            for (int o = 0; o < numberOfIslands; o++)
            {
            
                //Vygeneruj náhodné číslo mezi 0 a maximálním počtem hexagonů a vymaž je ze zásobníku
                int randomNumber = Random.Range(o, hexagonCells.Length);

                var randomObject = new GameObject[numberOfIslands];
                randomObject[o] = hexagonCells[randomNumber];

                hexagonCells[randomNumber] = hexagonCells[o];
                hexagonCells[o] = randomObject[o];
 
            }
        }
        */
        
    }
    void CreateHexagonCell(int x,int z,int i) 
    {
        HexagonCell hexCellInfo = hexagonCellPrefab.GetComponent<HexagonCell>();

        hexagonPosition.x = (x + z * 0.5f - z/2) * (hexCellInfo.innerHexagonRadius * 2f);
        hexagonPosition.y = 0;
        hexagonPosition.z = z * (hexCellInfo.outerHexagonRadius * 1.5f);

        GameObject hexCell = hexagonCells *= Instantiate<GameObject>(hexagonCellPrefab) as GameObject;*

cellPositions = hexagonPosition;
hexCell.transform.SetParent(transform, false);
hexCell.transform.localPosition = hexagonPosition;

}
}

public GameObject GetNeighbours(int x, int z)
{
List neighbours = new List(6);

    // Left (G)
    if( x > 0 ) neighbours.Add(hexagonCells[x - 1 + z * mapSize]);

    // Right (D)
    if( x < mapSize ) neighbours.Add(hexagonCells[x + 1 + z * mapSize]);

    if(z % 2 == 0)
    {
        // Top Left (B)
        if( z > 0 ) neighbours.Add(hexagonCells[(x - 1) + (z - 1) * mapSize]);

        // Top Right (C)
        if( z > 0 ) neighbours.Add(hexagonCells[x + (z - 1) * mapSize]);
        
        // Bottom Left (F)
        if( z < mapSize ) neighbours.Add(hexagonCells[(x - 1) + (z + 1) * mapSize]);

        // Bottom Right (E)
        if( z < mapSize ) neighbours.Add(hexagonCells[x + (z + 1) * mapSize]);
    }
    else
    {
        // Top Left (B)
        if( z > 0 ) neighbours.Add(hexagonCells[x + (z - 1) * mapSize]);

        // Top Right (C)
        if( z > 0 ) neighbours.Add(hexagonCells[(x - 1) + (z - 1) * mapSize]);
        
        // Bottom Left (F)
        if( z < mapSize ) neighbours.Add(hexagonCells[x + (z + 1) * mapSize]);

        // Bottom Right (E)
        if( z < mapSize ) neighbours.Add(hexagonCells[(x - 1) + (z + 1) * mapSize]);
    }

    return neighbours.ToArray();
}

Trivial way to do it would be to add all hex cells to a list as you create them, then once they are all created you can loop through the list and check the distance to each cell and mark as a neighbor if that distance is small enough. Not the most efficient way but as long as you don’t have thousands of cells it shouldn’t be too bad.

foreach (GameObject cell in cellList){
    foreach (GameObject otherCell in cellList)
    {
        if(cell != otherCell){
            if(Vector3.Distance(cell, otherCell) <= cellDistance){
                //Add otherCell to cell's list of neighbors
            }
        }
    }
}

NOTE- You should only calculate this ONCE at startup. I recommend adding a script to each cell that keeps track of it’s neighbors, because recalculating at run time every time you need to find the neighbors will be very inefficient.