cellular automata, adding a layer of new tiles.

So I’ve been trying to get 5 layers above my generated terrain, but I cant figure out how to do this.
Thnx in advance.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Tilemaps;

public class ProceduralGeneration : MonoBehaviour
{
    [Header("Terrain Gen")]
    [SerializeField] int width, height;
    [SerializeField] float smoothness = 50;
    [SerializeField] int dirtLayerHeight = 5;


    [Header("Caves")]
    [Range(0, 100)]
    [SerializeReference] int randomFillPercent;
    [Range(0, 10)]
    [SerializeField] int smoothAmount;

    int[] perlinHeightList;

    [SerializeField] TileBase stoneTile, caveTile, groundTile;
    [SerializeField] Tilemap stoneTilemap, caveTileMap, groundTilemap;

    [SerializeField] float seed;



    int[,] map;

    private void Start()
    {
        perlinHeightList = new int[width];
        Generation();
    }

    void Generation()
    {
        seed = Random.Range(10000, -10000);
        stoneTilemap.ClearAllTiles();
        map = GenerateArray(width, height, true);
        map = TerrainGeneration(map);
        SmoothMap(smoothAmount);
        RenderMap(map, stoneTilemap, caveTileMap, stoneTile, caveTile);
    }

    public int[,] GenerateArray(int width, int height, bool empty)
    {
        int[,] map = new int[width, height];
        for(int x = 0; x < width; x++)
        {
            for(int y = 0; y < height; y++)
            {
                map[x, y] = empty ? 0 : 1;
            }
        }
        return map;
    }

    bool IsInMapRange(int x, int y)
    {
        return x >= 0 && x < width && y >= 0 && y < height;
    }

    public int[,] TerrainGeneration(int[,] map)
    {
        System.Random pesudoRandom = new System.Random(seed.GetHashCode());
        int perlinHeight;
        for(int x = 0; x < width; x++)
        {
            perlinHeight = Mathf.RoundToInt(Mathf.PerlinNoise(x / smoothness, seed) * height / 2);
            perlinHeight += height / 2;
            perlinHeightList[x] = perlinHeight;

            for (int y = 0; y < perlinHeight; y++)
            {
              map[x, y] = (pesudoRandom.Next (1,100) < randomFillPercent) ? 1 : 2;
            }
        }
        return map;
    }

    void SmoothMap(int smoothAmount)
    {
        for (int i = 0; i < smoothAmount; i++)
        {
            for (int x = 0; x < width; x++)
            {
                for (int y = 0; y < perlinHeightList[x]; y++)
                {
                    if (x == 0 || y == 0 || x == width - 1 || y == perlinHeightList[x] - 1)
                    {
                        map[x, y] = 1;
                    }
                    else
                    {
                        int surroundingGroundCount = GetSurroundingGroundCount(x, y);
                        if (surroundingGroundCount > 4)
                        {
                            map[x, y] = 1;
                        }
                        else if (surroundingGroundCount < 4)
                        {
                            map[x, y] = 2;
                        }
                    }
                }
            }
        }
    }

    int GetSurroundingGroundCount(int gridX, int gridY)
    {
        int groundCount = 0;
        for (int nebX = gridX - 1; nebX <= gridX+1; nebX++ )
        {
            for (int nebY = gridY - 1; nebY <= gridY+ 1; nebY++)
            {
                if (IsInMapRange(nebX, nebY))
                {
                    if (nebX != gridX || nebY != gridY)
                    {
                        if (map[nebX,nebY] == 1)
                        {
                            groundCount++;
                        }
                    }
                }
            }
        }
        return groundCount;
    }

    public void RenderMap(int[,] map, Tilemap stoneTilemap, Tilemap caveTileMap, TileBase stoneTilebase, TileBase caveTilebase)
    {
        for (int x = 0; x < width; x++)
        {
            for (int y = 0; y < height; y++)
            {
                if (map[x, y] == 1)
                {
                    stoneTilemap.SetTile(new Vector3Int(x, y, 0), stoneTilebase);
                    //caveTileMap.SetTile(new Vector3Int(x, y, 0), caveTilebase);
                }
                else if (map[x, y] == 2)
                {
                    caveTileMap.SetTile(new Vector3Int(x, y, 0), caveTilebase);
                }
            }
        }
    }

    void clearMap()
    {
        stoneTilemap.ClearAllTiles();
        caveTileMap.ClearAllTiles();
    }
}

So I added:

                if (y < perlinHeightList[x] - dirtLayerHeight)
                {
                    stoneTilemap.SetTile(new Vector3Int(x, y, 0), stoneTilebase);
                }

To

    public void RenderMap(int[,] map, Tilemap stoneTilemap, Tilemap caveTileMap, TileBase stoneTilebase, TileBase caveTilebase)
    {
        for (int x = 0; x < width; x++)
        {
            for (int y = 0; y < height; y++)
            {
                if (map[x, y] == 1)
                {
                    //stoneTilemap.SetTile(new Vector3Int(x, y, 0), stoneTilebase);
                    //caveTileMap.SetTile(new Vector3Int(x, y, 0), caveTilebase);
                }
            else if (map[x, y] == 2)
                {
                    caveTileMap.SetTile(new Vector3Int(x, y, 0), caveTilebase);
                }
                if (y < perlinHeightList[x] - dirtLayerHeight)
                {
                    stoneTilemap.SetTile(new Vector3Int(x, y, 0), stoneTilebase);
                }
            }
        }
    }

And now I am getting the exact opposite of what I wanted…

So I now am able to get 5 blocks above the cave generation, I just need a way to fill in the blocks between the Top layer, and the cave layer

    public void RenderMap(int[,] map, Tilemap stoneTilemap, Tilemap caveTileMap, TileBase stoneTilebase, TileBase caveTilebase)
    {
        for (int x = 0; x < width; x++)
        {
            for (int y = 0; y < height; y++)
            {
                if (map[x, y] == 1 || y ==  perlinHeightList[x] + dirtLayerHeight)
                {
                stoneTilemap.SetTile(new Vector3Int(x, y, 0), stoneTilebase);
                //caveTileMap.SetTile(new Vector3Int(x, y, 0), caveTilebase);
                }
                else if (map[x, y] == 2 || y == perlinHeightList[x] + dirtLayerHeight)
                {
                caveTileMap.SetTile(new Vector3Int(x, y, 0), caveTilebase);
                }
            }
        }
    }

I found the sollution, it was to use the same loop as I used earlier