- Home /
How do you completely clear a mesh?
Hello,
The heading says it all. I have procedurally made a mesh and it works great, but when I go to clear it with "Mesh.Clear()", the mesh is still there. I have also passed false to the Clear function ( "mesh.Clear(false) ) and it doesn't work either. I want to clear it because this mesh is always changing, and the updates to the mesh are happening correctly, but the old part of the mesh is never cleared, resulting in a large messy mesh.
How can I just delete the mesh and start fresh from code?
Thanks
Did you try following up with;
RecalculateBounds(); Optimize();
I think this forces an update. As far as I am aware, Clear() removes the copy in memory but the changes are not immediately uploaded to the GPU. I'm not entirely sure you can submit a cleared mesh to the GPU.
Better to assign your mesh property to null if you want to visually indicate a lack of mesh data.
Once you have regenerated new data, assign it back to the the mesh property on your $$anonymous$$eshFilter.
Did you try Destroy(mesh)
; or mesh.Upload$$anonymous$$eshData(true);
(see).
Answer by highpockets · Nov 19, 2015 at 02:12 PM
Thanks for the suggestions. I was going about this the wrong way. I just needed to rewrite the vertex positions. My code was adding vertex positions to a list which then was passed to the vertex positions for the mesh, but I never cleared the list when I wanted to refresh, thus it was never clearing the old positions.
Just a note to anyone who wants to do this in the future. When you want the mesh to disappear without destroying the game object. Just use mesh.clear(false);
( this clears the data, but doesn't visually clear the mesh ), if you are using a list like myself to pass the vertex positions to the mesh then you also need to clear the list listName.Clear();
and then you just need to pass the empty list to the mesh vertex array mesh.vertices = listName.ToArray();
.