- Home /
Strange behavior when adding generated sharedMesh to MeshCollider
I have been trying to apply a generated mesh as the sharedMesh of a MeshCollider component. Basically I have a script that allows the user to click and drag the mouse to draw a rectangular grid highlighting a selection of 1x1 tiles. In Update() the script calls the function BuildMesh() (see below) which draws the mesh and applies it to the MeshFilter and MeshCollider. I need the MeshCollider to detect a right mouse button event on the mesh to deselect the rectangle and clear the mesh, but initially the MeshCollider did not appear to be detecting the event properly. I eventually discovered that if I manually disabled and re-enabled the MeshCollider component in the inspector it would suddenly work. I don't understand why I need to do this though. The following code works, but I can't figure out why the last two lines are necessary. Does anyone have any idea what sort of weirdness is going on behind the scenes that might cause this?
public void BuildMesh()
{
tri_list = new List<int>();
for (int z = _start_z; z <= _end_z; z++)
{
for (int x = _start_x; x <= _end_x; x++)
{
if (! (selected_tiles[x,z] == 1))
{
DrawTile(x, z);
}
}
}
mesh.Clear();
mesh.vertices = verts;
mesh.triangles = tri_list.ToArray();
mesh.normals = norms;
mesh.uv = uv;
mesh_filter.mesh = mesh;
mesh_collider.sharedMesh = mesh;
mesh_collider.enabled = false;
mesh_collider.enabled = true;
}
Your answer
Follow this Question
Related Questions
MeshCollider has different shape than MeshFilter 1 Answer
Sharing a generated mesh between multiple game objects 2 Answers
How do I clone a sharedMesh? 2 Answers
moving hole 3 Answers