- Home /
[Solved] Change the limits on a hingejoint?
Hello Devs,
I have a parent (cube) with four children (cube). The children are attached with the parent using a hingejoint. The connected body for all the children is the parent. Initially the angle on their limits are set to min -40 and max 40. But my requirement is at certain point I would like to change the limits to 0.
CODE
//C#
public Component[] hingeJoints;
void Start()
{
hingeJoints = GetComponentsInChildren<HingeJoint>();
}
void Update()
{
// At a certain instance
foreach (HingeJoint joint in hingeJoints)
{
joint.limits.min = 0;
joint.limits.max = 0;
}
}
ERROR:
error CS1612: Cannot modify a value type return value of `UnityEngine.HingeJoint.limits'. Consider storing the value in a temporary variable
EXPLANATION:
This code is attached to the parent and I am trying to access the hinge joints on their children.
Could you devs please help me with my mistake. I would be glad to hear some alternative solutions too, if you guys think of.
Thank you
Answer by Kryptos · Jul 31, 2012 at 08:14 AM
As you can read in the doc, JointLimits is a struct. Therefore you need to overwrite it completely. That is what the error means (Consider storing the value in a temporary variable).
JointLimits limits = hingeJoint.limits;
limits.min = 20;
hingeJoint.limits = limits;
Thanks $$anonymous$$ryptos!!! I missed that it was a structure.