Procedural Generated mesh collider creates invisible walls after the first iteration

I have the a procedural generated cave code from the unity tutorials. It works great the first time but as soon as I press ‘r’ (the update key) to generate a new cave the mesh collider creates invisible walls. The only thing I can come up with is that the old mesh collider is not being erased, but I dont know how to do that. Attached bellow is the relevant portion of the code I am using.

public class MarchingSquares : MonoBehaviour {

	public SquareGrid squareGrid;
	List<Vector3> vertices;
	List<int> triangles;
	public MeshFilter walls;
	//public bool is2D; 
	public static float wallHeight = 5;

	Dictionary<int,List<Triangle>> triangleDictionary = new Dictionary<int, List<Triangle>>();
	List<List<int>> outlines = new List<List<int>> ();
	HashSet<int> checkedVertices = new HashSet<int>();

	public void GenerateMesh(int[,] map, float squareSize){

		outlines.Clear ();
		checkedVertices.Clear ();
		triangleDictionary.Clear ();

		squareGrid=new SquareGrid(map,squareSize);

		vertices = new List<Vector3> ();
		triangles = new List<int> ();
		for (int x =0; x< squareGrid.squares.GetLength(0); x++) {
			for (int y=0; y< squareGrid.squares.GetLength(1); y++) {
				TrigangluateSqaure(squareGrid.squares[x,y]);
			}
		}

		Mesh mesh = new Mesh ();
		GetComponent<MeshFilter>().mesh = mesh;

		mesh.vertices = vertices.ToArray ();
		mesh.triangles = triangles.ToArray ();
		mesh.RecalculateNormals();
		CreateWallMesh ();
	}

	void CreateWallMesh(){

		CalculateMeshOutlines ();
		List<Vector3> wallVertices = new List<Vector3> ();
		List<int> wallTriangles = new List<int> ();
		Mesh wallMesh = new Mesh ();

		foreach (List<int> outline in outlines) {
			for (int i=0; i<outline.Count-1;i++){
				int startIndex = wallVertices.Count;
				wallVertices.Add(vertices[outline*]);//top left*
  •  		wallVertices.Add(vertices[outline[i+1]]);//bottom left*
    

wallVertices.Add(vertices[outline_]-Vector3.upwallHeight);//bottom left_
_ wallVertices.Add(vertices[outline[i+1]]-Vector3.upwallHeight);//bottom right_

* wallTriangles.Add (startIndex);*
* wallTriangles.Add (startIndex+2);*
* wallTriangles.Add (startIndex+3);*

* wallTriangles.Add (startIndex+3);*
* wallTriangles.Add (startIndex+1);*
* wallTriangles.Add (startIndex);*
* }*
* }*

* wallMesh.vertices = wallVertices.ToArray ();*
* wallMesh.triangles = wallTriangles.ToArray ();*
* wallMesh.RecalculateBounds();*
* walls.mesh = wallMesh;*

* MeshCollider wallCollider = walls.gameObject.AddComponent ();*
* wallCollider.sharedMesh.RecalculateBounds ();*
* wallCollider.sharedMesh.RecalculateNormals ();*
* //wallCollider.sharedMesh = wallMesh;*
* }*

Any help would be greatly appreciated

This is late, however I am sure others still have this issue and I have been able to fix it by removing this line and just getting the component:

MeshCollider wallCollider = walls.gameObject.AddComponent<MeshCollider> ();

Here is my CreateWallMesh() method:

void CreateWallMesh()
    {

        CalculateMeshOutlines();

        List<Vector3> wallVertices = new List<Vector3>();
        List<int> wallTriangles = new List<int>();
        Mesh wallMesh = new Mesh();
        float wallHeight = 5;
        MeshCollider wallCollider = walls.GetComponent<MeshCollider>();

        foreach (List<int> outline in outlines)
        {
            for (int i = 0; i < outline.Count - 1; i++)
            {
                int startIndex = wallVertices.Count;
                wallVertices.Add(vertices[outline*]); // left*

wallVertices.Add(vertices[outline[i + 1]]); // right
wallVertices.Add(vertices[outline_] - Vector3.up * wallHeight); // bottom left_
wallVertices.Add(vertices[outline[i + 1]] - Vector3.up * wallHeight); // bottom right

wallTriangles.Add(startIndex + 0);
wallTriangles.Add(startIndex + 2);
wallTriangles.Add(startIndex + 3);

wallTriangles.Add(startIndex + 3);
wallTriangles.Add(startIndex + 1);
wallTriangles.Add(startIndex + 0);
}
}
wallMesh.vertices = wallVertices.ToArray();
wallMesh.triangles = wallTriangles.ToArray();
walls.mesh = wallMesh;
wallCollider.sharedMesh = wallMesh;
}
You just have to make sure that the Walls object has a Mesh Collider before the game launches, rather than having the code generate it.