Modifying a mesh at runtime

I’m trying to modify a mesh at run time but so far it wont update.

public MeshFilter meshFilter;
	public bool change = false;
	private Mesh mesh;
	private Vector3[] vertices;
	// Use this for initialization
	void Start () {
		mesh = meshFilter.mesh;
		vertices = mesh.vertices;
		foreach(Vector3 vert in vertices){
			Ray ray = new Ray(new Vector3(transform.position.x+vert.x,transform.position.y+vert.y+5,transform.position.z+vert.z),Vector3.down);
			RaycastHit hit = new RaycastHit();
			if(Physics.Raycast(ray,out hit,5000f)){
				Vector3 temp = hit.point-(transform.position+vert);
				Debug.Log("vert before: "+vert);
				vert.Set(vert.x,temp.y+0.1f,vert.z);
				Debug.Log("vert after: "+vert);
			}
			mesh.vertices = vertices;
			mesh.RecalculateBounds();
			mesh.RecalculateNormals();
		}
	}

At the moment I’m just trying to get a plane to deform according to the ground, correct values are calculated but it doesnt matter even if I put in extreme values, the mesh just wont change. I’ve also tried setting the meshfilter.mesh to my new mesh but still the same results.

You can’t do a foreach loop like that on the vertices collection, Vector3 is a struct and so you are just modifying something that is then being immediately thrown away. You need to do a for loop and get and set the vector in it.