- Home /
Bones not keeping weights after Instantiate()?
Ok, so I create an object with an IDE defined mesh. I assign boneweights based on the position.x of each vertex.
Later, I instantiate the object, I then assign the boneweights to the mesh, however when I attempt to rotate the bones, they have nothing happens.
Here is the pertinent part of my project:
DUMMYWALL = Instantiate(DUMMYWALL,World.position,Quaternion.identity)as Transform;
Destroy(DUMMYWALL.gameObject.GetComponent<MeshRenderer>());
DUMMYWALL.gameObject.AddComponent<SkinnedMeshRenderer>();
//DUMMYWALL.localScale = Scale;
Transform[] WallBones = new Transform[2];
Matrix4x4[] WallBP = new Matrix4x4[2];
MeshFilter WallMeshFilter = DUMMYWALL.gameObject.GetComponent<MeshFilter>();
SkinnedMeshRenderer WallSMRenderer = DUMMYWALL.gameObject.GetComponent<SkinnedMeshRenderer>();
Vector3[] WallVerts = WallMeshFilter.mesh.vertices;
WallBW = new BoneWeight[WallVerts.Length];
for (int i = 0; i < WallVerts.Length; i++) {
WallBW[i].boneIndex0 = 0;
WallBW[i].weight0 = Mathf.Abs(10*WallVerts[i].x);
WallBW[i].boneIndex1 = 1;
WallBW[i].weight1 = (1f - (10*Mathf.Abs(WallVerts[i].x)));
}
WallBones[0] = new GameObject("Bone 0").transform;
WallBones[0].localPosition = WallVerts[189];
WallBones[0].parent = DUMMYWALL;
WallBP[0] = WallBones[0].worldToLocalMatrix * transform.localToWorldMatrix;
WallBones[1] = new GameObject("Bone 1").transform;
WallBones[1].localPosition = WallVerts[2];
WallBones[1].parent = DUMMYWALL;
WallBP[1] = WallBones[1].worldToLocalMatrix * transform.localToWorldMatrix;
//Assign collections to DUMMYWALL
WallMeshFilter.mesh.boneWeights = WallBW;
WallMeshFilter.mesh.bindposes = WallBP;
WallSMRenderer.sharedMesh = WallMeshFilter.mesh;
WallSMRenderer.bones = WallBones;
Transform southwall = (Transform)Instantiate(DUMMYWALL,(nCMfilter.mesh.vertices[0]*.99f),Quaternion.identity);
southwall.name = "SouthWall";
southwall.parent = nCGO.transform;
southwall.localRotation = (Quaternion.LookRotation(Vector3.zero - southwall.transform.position) * away);
MeshFilter SWMFilter = southwall.gameObject.GetComponent<MeshFilter>();
SkinnedMeshRenderer SWSMRenderer = southwall.gameObject.GetComponent<SkinnedMeshRenderer>();
Matrix4x4[] WallBP = new Matrix4x4[2];
SWMFilter.mesh.boneWeights = WallBW;
SWSMRenderer.sharedMesh = SWMFilter.mesh;
SWSMRenderer.bones[0].localPosition = SWMFilter.mesh.vertices[189];
SWSMRenderer.bones[1].localPosition = SWMFilter.mesh.vertices[2];
Not even a comment?
are boneweights included when you instantiate a previously defined object? It would seem to me they really would have to be...
Answer by Cyber_Defect · Apr 17, 2013 at 06:03 AM
I figured it out, I apparently have to set the weights of the instantiated mesh. Talk about a disgusting waste of processing.
or not, I just have to set it to the already defined boneweight[] collection...
Why doesn't this go with the instantiation?
I'$$anonymous$$ pretty aure it's because you use the .mesh property. It will create an instance for this $$anonymous$$eshFilter only. When you duplicate / instantiate the object it will probably use the shared$$anonymous$$esh and on this you never set the boneWeights. You have to test this yourself ;)
Well played Bunny, I hadn't even thought of that. The API uses the mesh property and I ran with it...
Your answer
![](https://koobas.hobune.stream/wayback/20220613100451im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Transfer bones from one Object to another (same meshes) 0 Answers
Second prefab(Clone) not finding Bones 0 Answers
Is there, or can u make a toggle button for disabling bones in Playmode? 0 Answers
Checking if object intersects? 1 Answer
Does the transform.parent hierarchy have any relationship to the bone hierarchy? 1 Answer