Set Vertices to Ground

I’m trying to set all the vertices of plane to match the height of the terrain. At the moment, it sets about half the plane to some random Y value and ignores the other half. I can’t seem to figure out what’s going wrong here.

So far this is what I have:

using UnityEngine;
using UnityEditor;
using System.Collections;

public class GroundVertices : MonoBehaviour
{
	
	[MenuItem("Tools/Ground Vertices")]
	static void Init()
	{
		Mesh m = (Mesh)((MeshFilter)Selection.activeTransform.gameObject.GetComponent(typeof(MeshFilter))).sharedMesh;
		Vector3[] verts = m.vertices;
		
		for(int i = 0; i < verts.Length; i++)
    	{    			
    		verts <em>= new Vector3(verts<em>.x, verts_.y + 100f, verts*.z);*_</em></em>

* RaycastHit ground = new RaycastHit();*
_ if(Physics.Raycast(verts*, Vector3.down, out ground))
{*_

* Debug.Log(ground.point.y + ground.transform.tag);*
_ verts*.y = ground.point.y;
}
} *_

* m.vertices = verts;*
* m.RecalculateNormals();*
* }*
}

Turns out that the issue with half the verts flattening themselves was a red herring. The issue was that I was modifying local points, not world coordinates. Here is my solution:

using UnityEngine;
using UnityEditor;
using System.Collections;

public class GroundVertices : MonoBehaviour
{
	
	[MenuItem("Tools/Ground Vertices")]
	static void Init()
	{
		GameObject go = Selection.activeTransform.gameObject;
		Mesh m = (Mesh)((MeshFilter)go.GetComponent(typeof(MeshFilter))).sharedMesh;
		Vector3[] verts = m.vertices;
		int layerMask = 1<<10;
		for(int i = 0; i < verts.Length; i++)
    	{    			
    		Vector3 castFrom = go.transform.TransformPoint(verts*);		*
  •  	RaycastHit ground = new RaycastHit();*
    
  •      if(Physics.Raycast(castFrom, -Vector3.up, out ground, Mathf.Infinity,layerMask))*
    
  •      {*
    

_ verts = go.transform.InverseTransformPoint(ground.point);_
* }*
* } *

* m.vertices = verts;*
* m.RecalculateBounds();*
* m.RecalculateNormals();*
* m.Optimize();*
* }*
}