2

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 MeshColliders in my script, so that they are removed and only the newly generated mesh remains?

Initial maze image

Initial maze image

Second maze image

Second maze image

Each maze, after the first, has the old MeshColliders represented by the green outline and it looks like it is just making new MeshColliders 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;
}
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291

1 Answers1

0

From here, you should call Mesh.Clear "before rebuilding triangles array."

mesh = new Mesh ();
cave.mesh.Clear();
cave.mesh = mesh;
Poosh
  • 532
  • 2
  • 10
  • 25
  • I think I found the source of the problem, what is happening is that in my map generator script it calls on the program listed above which creates a MeshCollider `MeshCollider wallCollider = walls.gameObject.AddComponent ();` what I need to do is have this only happen the first time and not happen on each successive click – Paul Funderburker Jun 16 '17 at 15:20