Unable to connect perlin terrains properly

Hi. I am trying to build up my terrain in segments using perlin noise. The segments are lined up next to eachother, but they are not connecting properly. I’ve set up each terrains neighbor and called Flush(). I am guessing it has something to do with the TerrainData?I will appreciate any help I can get.

The problem. As you can see, there’s a gap between most segments:

Here’s the main code

public class MapGenerator : MonoBehaviour {
	
	int segmentSize = 256;

	int terrainRadius = 1; // not including the center-piece
	int terrainArraySize = 0;	
	Vector3 terrainSize;
	int perlinSeed = 1;
	GameObject[,] terrainSegments;

	
	void Start () {
		
		terrainSize = new Vector3(segmentSize,segmentSize,segmentSize);		
		terrainArraySize = terrainRadius * 2 + 1;
		terrainSegments = new GameObject[terrainArraySize,terrainArraySize];				
		
	}
	
	// Update is called once per frame
	void Update () {
		if(Input.GetKeyUp(KeyCode.E)){
			GenerateTerrain();	
		}

	}
	
	void GenerateTerrain(){
		
		for(int i = 0; i < terrainArraySize;i++){ // column
		
			for(int j = 0;j< terrainArraySize;j++){ // row
			
				Vector3 offset = new Vector3(i, 0f, j);
				
				HeightMap h = new HeightMap(segmentSize, perlinSeed);
				h.AddPerlinNoise(3.0f, offset);
				
				terrainSegments[i,j] = InstantiateTerrainSegment(h.Heights, new Vector3(offset.x * segmentSize, offset.y * segmentSize, offset.z * segmentSize));
				
			}
			
		}

		// setup neightbors
		for(int i = 0; i < terrainArraySize;i++){ // column
		
			for(int j = 0;j< terrainArraySize;j++){ // row
			
				// Maximum potential neighbors is 4
				// Corner-pieces only have 2 neighbors
				// Edge-pieces have 3 neighbors
				
				Terrain left = null, top = null, right = null, bottom = null;
				int countN = 0;
				
				if(i - 1 >= 0){ // left
					left = (terrainSegments[i-1,j].GetComponent<Terrain>());
					countN++;
				}
				
				if(i + 1 < terrainArraySize){ // right
					right = (terrainSegments[i+1,j].GetComponent<Terrain>());
					countN++;
				}
				
				if(j - 1 >= 0){ // top
					top = (terrainSegments[i,j-1].GetComponent<Terrain>());
					countN++;
				}
				if(j + 1 < terrainArraySize){ // bottom
					bottom = (terrainSegments[i,j+1].GetComponent<Terrain>());
					countN++;
				}
				
				terrainSegments[i,j].GetComponent<Terrain>().SetNeighbors(left, top, right, bottom);
								
			
			}
			
		}
		
		// finally, when neighbors are set, flush the terrains
		for(int i = 0; i < terrainArraySize;i++){ // column
		
			for(int j = 0;j< terrainArraySize;j++){ // row
			
				terrainSegments[i,j].GetComponent<Terrain>().Flush();
			
			}
			
		}
		
	}
	
	GameObject InstantiateTerrainSegment(float[,] heights, Vector3 position){
		
		TerrainData td = new TerrainData();
		
		td.heightmapResolution = 257;
		td.size = terrainSize;
		td.heightmapResolution = 256;
		td.baseMapResolution = 256;
		
		td.SetDetailResolution(256, 16);
		
		td.SetHeights(0, 0, heights);
		
		
		GameObject t = Terrain.CreateTerrainGameObject(td);
		t.transform.position = position;
		
		return t;
	}
}

And the heightmap function:

public void AddPerlinNoise(float f, Vector3 offset)
 {
  for (int i = 0; i < Size; i++)
  {
   for (int j = 0; j < Size; j++)
   {
		float x = ((float)i / (Size - 1) + offset.z);
		float y = ((float)j / (Size - 1) + offset.x);
		float z = 0f;			

	Heights[i, j] = Perlin.Noise(x, y, z);
				
   }
  }
 }

Solved.

I ended up changing a whole lot, but the problem was indeed the terrainData. Unity set the heightmapresolution to power of 2 + 1

So I basicly changed the following values:

td.heightmapResolution = 256; // if I set this as 257 Unity will up it to 513. But if I set it to 256 unity will change it to 257
td.heightmapResolution = 257;
td.baseMapResolution = 257;

Also, there was a bug in the neighborscript.