Instantiate fresh duplicate

Hello. I am using modular planes as a floor where player is changing vertex color (from green to black) of that plane as he moves. The issue is, since the vertex color script basically rewrites the mesh, that when I have for exampe floor with 10x10 blocks of the same floor, changes that player does on a plane that he is standing on are projected to all other planes as well because it use the same source mesh.

Is it possible by script to solve this? Somehow create temporary duplicates on start?

I am also attaching screenshot to better explain the issue:

You could try something like this…

Mesh meshInstance;
void Awake()
{
  MeshFilter mf = GetComponent<MeshFilter>();
  meshInstance = Instantiate(mf.sharedMesh);
  mf.sharedMesh = meshInstance;
}
void OnDestroy()
{
  DestroyImmediate(meshInstance);
}

Cheers! that worked.

Firstly it did not work but I forgot to replace mesh in meshcollider as well which is used for vertex change.

Thank you!

1 Like