I followed a tutorial on making a procedural generated maze in unity using C# that when you click the mouse button, it generates a new maze.
However, when I generate a new maze everything looks right but the MeshCollider
for the old maze mesh is still in place, in addition to the new maze MeshCollider
making numerous invisible walls.
How would I clear the old MeshCollider
s in my script, so that they are removed and only the newly generated mesh remains?
Initial maze image
Second maze image
Each maze, after the first, has the old MeshCollider
s represented by the green outline and it looks like it is just making new MeshCollider
s and not replacing the old one with a new one. I thought I could fix this by setting the mesh and MeshCollider
to null, then new mesh at the beginning of each new maze generation, but it does nothing:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Mesh_Generator : MonoBehaviour {
public SquareGrid squareGrid;
public MeshFilter walls;
public MeshFilter cave;
public bool is2D;
List<Vector3> vertices;
List<int> triangles;
Dictionary<int,List<Triangle>> triangleDictionary = new Dictionary<int, List<Triangle>> ();
List<List<int>> outlines = new List<List<int>> ();
HashSet<int> checkedVerticies = new HashSet<int> ();
public void GenerateMesh(int[,] map, float squareSize){
triangleDictionary.Clear ();
outlines.Clear ();
checkedVerticies.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++) {
TriangulateSquare (squareGrid.squares [x, y]);
}
}
Mesh mesh = null;
mesh = new Mesh ();
cave.mesh = mesh;
mesh.vertices = vertices.ToArray ();
mesh.triangles = triangles.ToArray ();
mesh.RecalculateNormals ();
if (!is2D) {
CreateWallMesh ();
}
}
void CreateWallMesh(){
CalculateMeshOutLines ();
List<Vector3> wallVerticies = new List<Vector3> ();
List<int> wallTriangles = new List<int> ();
Mesh wallMesh = new Mesh ();
float wallHeight = 5;
foreach (List<int> outline in outlines){
for (int i = 0; i < outline.Count -1; i ++){
int startIndex = wallVerticies.Count;
wallVerticies.Add (vertices [outline [i]]); // left vertex
wallVerticies.Add (vertices [outline [i+1]]); // right vertex
wallVerticies.Add (vertices [outline [i]] - Vector3.up * wallHeight); // bottom left vertex
wallVerticies.Add (vertices [outline [i+1]] - Vector3.up * wallHeight); // bottom right vertex
wallTriangles.Add (startIndex + 0); //triangle 1
wallTriangles.Add (startIndex + 2);
wallTriangles.Add (startIndex + 3);
wallTriangles.Add (startIndex + 3); //triangle 2
wallTriangles.Add (startIndex + 1);
wallTriangles.Add (startIndex + 0);
}
}
wallMesh.vertices = wallVerticies.ToArray ();
wallMesh.triangles = wallTriangles.ToArray ();
walls.mesh = wallMesh;
MeshCollider wallCollider = walls.gameObject.AddComponent<MeshCollider> ();
wallCollider = null;
wallCollider = new MeshCollider ();
wallCollider.sharedMesh = wallMesh;
}