- Home /
How do I clone a sharedMesh?
I have an editor script that clones an object and modifies the mesh of the clone. When I modify the sharedMesh, it changes not only the clone, but also the original. If I modify the mesh, the mesh collider does not change to match it.
How do I take the modified mesh, tell unity to define a new sharedMesh based on the modified mesh, and save it as a new prefab, probably using the AssetDatabase?
Answer by alwynd · Oct 13, 2013 at 03:12 PM
Hi, sorry for just pasting code, was a sloppy answer, the best way to do this is to clone the shared mesh, and to assign the clone, back to the sharedmesh property of the skinnedmeshrenderer...
myRenderer.sharedMesh = (Mesh) Instantiate( myRenderer.sharedMesh );
That way, the shared mesh is no longer an instance of the original.
Hope this helps!
When I do this, and attempt to create a prefab of my object that uses the cloned mesh, the prefab has null ins$$anonymous$$d of an actual mesh value. What should I do to have my new mesh stored in the prefab?
You should: 1. Add you new mesh into the prefab (embed it into it) 2. Clone this prefab in scene and assign mesh, stored in prefab to this cloned prefab to the corresponding meshfilter 3. Apply the prefab
All steps can be done from script.
Hi i am getting same problem, can you explain in details? Thanks!
Answer by Delmadan · May 02 at 11:04 PM
It may be more straightforward to do something like this:
var newMesh = new Mesh()
{
vertices = mesh.vertices,
triangles = mesh.triangles,
normals = mesh.normals,
tangents = mesh.tangents,
bounds = mesh.bounds,
uv = mesh.uv
};
so you're essentially just copying over the details of the original mesh to a new Mesh instance.
Your answer
![](https://koobas.hobune.stream/wayback/20220613092640im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Strange behavior when adding generated sharedMesh to MeshCollider 0 Answers
Query if MeshFilter is instantiated or shared? 1 Answer
Creating a Mesh asset inside the editor 1 Answer
Pointcast? - Raycast Point X from same Point X 0 Answers
How to make a trigger when a Collider hits a different collider 3 Answers