- Home /
I can't set the bone weights at runtime
pieceMesh.boneWeights[0].weight0 = 0.0f;
pieceMesh.boneWeights[0].weight1 = 1.0f;
pieceMesh.boneWeights[0].weight2 = 0.0f;
pieceMesh.boneWeights[0].weight3 = 0.0f;
print(pieceMesh.boneWeights[0].weight0);
This outputs "0.6888725" which is the original bone weight. It should be zero. I don't know what's going wrong here. The docs say
"Each vertex can be affected by up to 4 different bones. All 4 bone weights should sum up to 1."
so I gave weight1 1 but it doesn't look like that's the problem.
I would guess that you need to create a new instance of the weights array, change the values you want, and then pass it back to the mesh.
Answer by Bunny83 · Mar 14, 2015 at 06:05 AM
boneWeights is a property like all the other array in the Mesh class as well. That means when you read-access the property you get a copy of the internal array back. Changing something in that array won't change the actual data. You have to assign the array back to the property:
// C# / UnityScript
var weights = pieceMesh.boneWeights;
weights[0].weight0 = 0.0f;
weights[0].weight1 = 1.0f;
weights[0].weight2 = 0.0f;
weights[0].weight3 = 0.0f;
pieceMesh.boneWeights = weights;
print(pieceMesh.boneWeights[0].weight0);
Ah that works, thank you. I feel like I should have known that