- Home /
 
change lowTwistLimit.limit at runtime?
hey, how can i change the lowTwistLimit.limit on a character joint to zero? i tried:
 gameObject.GetComponent<CharacterJoint>().lowTwistLimit.limit = 0;
 
               but that results in "Cannot modify a value type return value of `UnityEngine.CharacterJoint.swing1Limit'. Consider storing the value in a temporary variable"
Answer by HarshadK · Jun 02, 2017 at 11:16 AM
The lowTwistLimit is of type [SoftJointLimit][1] which is a struct. So you can not modify just a single value of it as you are trying to do. This is similar to what happens when you try to update just one value of a transform's position since position is a Vector3 which is a struct. So you need to build a new struct with your desired values and assign it. Something like:
 CharacterJoint charJoint = gameObject.GetComponent<CharacterJoint>();
 SoftJointLimit jointLimit = charJoint.lowTwistLimit;
 jointLimit.limit = 0;
 // Here we assign the updated struct back to lowTwistLimit of character joint.
 charJoint.lowTwistLimit = jointLimit;
 
               [1]: https://docs.unity3d.com/ScriptReference/SoftJointLimit.html
Your answer
 
             Follow this Question
Related Questions
Unity Bug? Character joints NOT working after simple manual repositioning 1 Answer
How do I select a joint component from a GameObject that has multiple joint components? 1 Answer
Character Joint- Enable Projection...? 1 Answer
Joints... Joints... Meets Kinematic 0 Answers
Joint connected body not working 1 Answer