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?

}

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.