[Solved] Mesh Doesn’t Reset Upon Leaving Game Mode

Hello,
I posted this same question on Unity Answers a couple days ago, but had no luck there so I thought I’d try here as well.

I’m having a problem with real-time mesh editing.

I’m making a script that removes triangles from a mesh when you click them. Currently, it removes the triangle you click on and everything works great, however when I stop the game and go back to scene mode, the mesh doesn’t reset and the missing triangles in the mesh persist. Even if I start testing in game mode again, the missing triangles are still there. The only way I’ve found to reset the mesh is to entirely restart Unity. Even if I save the scene before restarting it resets the meshes to the way they should be, so it doesn’t save the edited meshes in any way. It appears to be some sort of strange bug.

Also, I’ve tried instancing a prefab of the mesh, but even then, the problem persists.

What’s causing this? It’s quite frustrating. Thanks for any help.

Just to be clear, are you saying that:

  1. You want the triangle removals made in game mode to be permanent, but when you revert to edit mode the triangles are reinstated, or…

  2. You want the removed triangles to be reinstated when you revert to edit mode, but instead the edits you make in game mode are persisting in edit mode?

(The phrase ‘the missing triangles persist’ is a little ambiguous, which is why I’m asking.)

Sorry for the confusion, I guess I worded that a bit weird. My problem is the latter of the two.

Hello,

I’d need to see code, but it sounds like you’re modifying a mesh in Unity’s cache – and since assets(as opposed to objects in game) do not revert when you stop testing, the changes are retained.

Best wishes,

-Lincoln Green

I’ve tried both using a non-asset sphere that only existed in the scene hierarchy as well as a prefabbed asset and both had the same result.

Anyway, here’s what I’m doing to remove a triangle from my mesh(es):

...
Mesh mesh = meshCollider.sharedMesh;
Vector3[] verts = mesh.vertices;
int[] tris = mesh.triangles;

mesh.Clear();

tris = removeTriangle(hit.triangleIndex, tris);

mesh.vertices = verts;	
mesh.triangles = tris;

meshCollider.sharedMesh = mesh;
...

removeTriangle():

private int[] removeTriangle(int triangle, int[] tris) {
	for (int i = triangle*3; i < tris.Length-3; ++i) {
		if (tris[i] == -1) break;
		tris[i] = tris[i+3];
	}
	return tris;
}

So basically it just calls removeTriangle(), which removes the three indices that make up said triangle, then sets the mesh to the new triangle array.

Obviously this code isn’t perfect or anything, I’m just trying to get the basics working.

Anyway, is there something I’m doing wrong? Is there a better way of accomplishing this?

Please excuse me if I’m doing this completely wrong, I’m pretty new to Unity.

This may not be relevant, but have you tried using meshCollider.mesh rather than meshCollider.sharedMesh?

Yes, I tried that. It had the same result, but this time it warned me that “mesh” was deprecated and that I should use “sharedMesh” instead, or something along those lines.

Thanks for the input though. :slight_smile:

Are you using Unity 2.x, or Unity 3?

I’m using 2.x.

Hm, I’ve written a lot of procedural mesh code in Unity and have never gotten an error message of that sort. Could you post the exact error message and the code that generated it?

It doesn’t give me an error, it’s a warning that says “mesh” is obsolete.

Ah, I missed that you’re using meshCollider.mesh. Are you trying to modify the mesh visual, or the mesh’s collision properties? If the former, you probably want to be modifying the mesh returned by MeshFilter.mesh, not the MeshCollider mesh.

OH DUH…And that, of course, solved the problem. Thanks a million, Jesse, and thanks to everyone else for your help as well :smile:. Don’t I feel dumb now, haha.