There are two reasons for this:
1.JointLimits
is a struct.
2.JointLimits
is declared as auto-property variable.
HingeJoint.limits
is a type of JointLimits
which is a struct
and declared as auto-property ({ get; set; }
) so you can't modify a variable(min
) that is inside it directly. You have to make a copy of the struct
, modify the variable inside it then assign the struct back to HingeJoint
. The use of struct
and the auto property to declare the limits variable is why you can't do that.
public JointLimits limits { get; set; }
The-same thing applies to transform.position
. You can't modify its x, y, z variables directly because position is a type of Vector3
which is a struct
and it is also declared as auto-property:
public Vector3 position { get; set; }
You have to make a copy of it first, modify it then assign it back to transform.position
.
Another unrelated problem in your code is the Start
function. It is Start
not start
.
HingeJoint hinge;
void Start()
{
hinge = GetComponent<HingeJoint>();
//Make Limit copy
JointLimits limits = hinge.limits;
//Modify limit variable
limits.min = 0;
//Assign back to HingeJoint
hinge.limits = limits;
}