Need help with manipulating meshes

I have a hexmap that consists of individual hexagon objects. The map is overlayed on a terrain. Each hexagon consists of 7 meshes: 6 sides and the central face. I am trying to get the vertices of the hexagons to match up with the height of the terrain to which they correspond. For example, if a vertex of the hexagon is at the world origin, and the terrain at the origin is at a height of 50, I want that vertex to be set to the same height(50).

Below is the code I have written so far. The entire script is used to handle the generation of the hexmap and setting of vertex heights of each hexagon. The snippet below is the portion of the code I was trying to use to set the vertex heights. It doesn’t even come close to working. After it executes, all of the hexagons look like lines. Could someone please help me re-write this bit so it works as intended?

			//Match vertices to terrain
			var filters = newTile.GetComponentsInChildren(MeshFilter);
			var mesh : Mesh;
			var verts : Vector3[];				

			for(var filter : MeshFilter in filters)
			{
				mesh = filter.mesh;
				verts = mesh.vertices;				

				for(var vert : Vector3 in verts)
				{
					vert.y = Terrain.activeTerrain.SampleHeight(vert);
				}					

				mesh.vertices = verts;				

				mesh.RecalculateBounds();
				mesh.RecalculateNormals();
			}

Vertices are defined in local coordinates, thus you should convert each one to world coordinates to get the terrain height at its position. But there are other problems: since you’re relocating only the vertices, the object’s position will remain the same - and you may end with a strange object whose pivot is well above or below all of their vertices. Another thing: is your object really composed by 7 meshes? Usually there’s only a single mesh, and the faces are composed by one or more triangles - the Unity cube, for instance, is a single mesh composed by 12 triangles.

Anyway, I think you should get the object’s position and adjust its Y coordinate according to the terrain height. Then get each vertex, convert to world space, find the height and convert it back to local space before storing it.

The script below does the job for a single mesh: specify the object’s transform in obj, and the desired position above the terrain in offsetY. NOTE: The object must have a MeshCollider, which will be updated to fit the new shape.

function LandMesh(obj: Transform, offsetY: float){
  var baseY = Terrain.activeTerrain.GetPosition().y + offsetY;
  var pos = obj.position;
  pos.y = Terrain.activeTerrain.SampleHeight(pos)+baseY;
  obj.position = pos; // define new object's position
  var mesh = obj.GetComponent(MeshFilter).mesh;
  var verts = mesh.vertices;
  for (var i = 0; i < verts.length; i++){
    var w = obj.TransformPoint(verts*); // get vertex in world space*

var h = Terrain.activeTerrain.SampleHeight(w) + baseY;
w.y = w.y - pos.y + h; // find the new height
verts = obj.InverseTransformPoint(w);
}
mesh.vertices = verts; // update the vertices…
mesh.RecalculateBounds(); // bounds…
mesh.RecalculateNormals(); // and normals
// update the mesh collider too
obj.GetComponent(MeshCollider).sharedMesh = mesh;
}

function Start(){
LandMesh(transform, 3);
}