Have seperate data on each Tile in a Tilemap?

Im trying to make a tile that is able to store its neighbours in a list to alter be able to get this list for my pathfinding code.

However, I cant seem to figure out how to make each tile have a seperate list of neighbours.

My guess is this has something to do with every tile originating from the same scriptable object? If so, how would I fix this and allow each tile to have seperate data?

Here is my code:

public List<TileBase> neighbours;
private Vector3Int cellPosition;

private readonly Vector3Int[] neighbourPositions =
{
    Vector3Int.up,
    Vector3Int.right,
    Vector3Int.down,
    Vector3Int.left,

    // if you also wanted to get diagonal neighbours
    //Vector3Int.up + Vector3Int.right,
    //Vector3Int.up + Vector3Int.left,
    //Vector3Int.down + Vector3Int.right,
    //Vector3Int.down + Vector3Int.left
};

public override void RefreshTile(Vector3Int position, ITilemap tilemap)
{
    base.RefreshTile(position, tilemap);
    Tilemap map = tilemap.GetComponent<Tilemap>();
    GridLayout grid = map.GetComponent<GridLayout>();
    cellPosition = position;

    if (!map.HasTile(cellPosition))
    {
        Debug.LogWarning($"The position {cellPosition} does not exist in the map!");
        return;
    }

    var sTiles = new List<TileBase>();
    foreach (var neighbourPosition in neighbourPositions)
    {
        var pos = cellPosition + neighbourPosition;

        if (map.HasTile(position))
        {
            var neighbour = map.GetTile(position);
            sTiles.Add(neighbour);
        }
    }

    neighbours = sTiles;
}

And here is a picture of how it looks in unity currently:

So you know, you can post/edit code using code-tags which is much easier to read and includes line numbers for reference too.

1 Like

Thanks! Fixed! :slight_smile: