- Home /
[Solved] Changing mesh of child gameobject
Hello,
I want to change the mesh of a gameobject which is the child of another gameobject. I locate the mesh, copy it, change it, clear the old mesh and replace it, as described in the script reference but the changes don't seem to take effect.
Is it because it's a child object? Here's my code briefly, I feel like it should work:
Transform parentTransform;
Transform childTransform;
Mesh mesh;
Vector3[] vertices;
Vector3 vert;
Vector2[] uv;
int[] triangles;
...
childTransform = parentTransform.Find(childname)
mesh = childTransform.gameObject.GetComponent<MeshFilter>().mesh;
vertices = mesh.vertices;
uv = mesh.uv;
triangles = mesh.triangles;
vert = vertices[0];
vert.y += 5; // Change y of a single vert
vertices[0] = vert;
mesh.Clear(); // Clear old mesh
mesh.vertices = vertices; // Assign new vertices with that one altered point
mesh.uv = uv;
mesh.triangles = triangles;
childTransform.gameObject.GetComponent<MeshFilter>().mesh = mesh; // Not sure if necessary
Should this, in principle, work like this? Or do I have to unparent all child objects first or something like that?
Answer by ffxz7ff · Nov 02, 2013 at 09:05 AM
Well, I found my answer. Adding the solution incase anyone ever runs into the same problem:
I modified the vertices of the child objects, but I was actually displaying the parent mesh, which was a combined mesh made of the child meshes. I then modfied the parent mesh instead and everything worked fine.
Your answer
Follow this Question
Related Questions
Parent component affect child objects 0 Answers
How can I change the lookout of an object? 0 Answers
Change mesh to to GameObject 1 Answer
transform.position = new Vector3 Bug or error 0 Answers
Switch for children not working 1 Answer