- Home /
Way to erase meshfilter mesh and rewrite it?
for some reason, when I have to delete some procedural mesh and write a new one over it, the old one doesn't disappear, even when I do destroyimmediate. with fixed update, and clear()... the mesh is still there!
function renewMesh();{
var meshy = GetComponent(MeshFilter).sharedMesh;
DestroyImmediate(meshy, true);
yield WaitForFixedUpdate();
var mesh : Mesh = GetComponent(MeshFilter).mesh;
mesh.Clear();// Clears all the data that the mesh can contain previously.
geometry= new Mesh();
GetComponent(MeshFilter).mesh = geometry;
ConstructMesh();//function makes fine mesh on first time only and then shows only first mesh that was destroyed and not new one?
}
even when i destroy ($$anonymous$$eshFilter) and add a new one, and run construct$$anonymous$$esh() the object still has an old mesh... what could be happening?
Answer by Dave-Carlile · May 04, 2013 at 04:06 AM
You need to specifically set the mesh to null before setting a new value...
var meshFilter : MeshFilter = GetComponent(MeshFilter);
meshFilter.mesh = null;
meshFilter.mesh = new Mesh();
It's counterintuitive, but at least the last I check it's what you have to do if you want to completely replace the mesh object.
But, you don't actually need to completely replace it. You can clear it, then just reset the mesh components with the new values (vertices, colors, etc.).
Also, your first line of code looks odd...
function renewMesh();{
}
I'm not much of a Javascript coder, but it seems like that semicolon would cause you some issues.
But, you don't actually need to completely replace it. You can clear it, then just reset the mesh components with the new values (vertices, colors, etc.).
I would really like a small example on that, or more explanation, this is exactly my problem.. Really need a way to only change some of the mesh, ins$$anonymous$$d of all of it with Destroy(); - have already tried all ways I can think about :(
if you need to change just some of the mesh probably best use generic list, or otherwise use normal arrays and clear it and rewrite it.