Been trying to modify the translation limits of these new Slider Joint 2D’s. I add them onto gameobjects at runtime, so I need to do it via script. These are two things I’ve tried, but don’t seem to work. Any help is appreciated!
sliderJoint.limits.max = targetLength;
error CS1612: Cannot modify a value type return value of `UnityEngine.SliderJoint2D.limits’. Consider storing the value in a temporary variable
sliderJoint.limits = new JointTranslationLimits2D(0,targetLength);
UnityEngine.JointTranslationLimits2D' does not contain a constructor that takes 2’ arguments
I’m not familiar with Slider Joints, but for this type of situation you have a few options.
- Copy the existing thing you want to change (here it’s sliderJoint.limits) into a new variable, change the part(s) you want, then assign it back.
- Create a new thing of the type you want, set its values, then assign it to the object you want to change.
Here’s how option 1 might look:
// 1) Copy it.
JointTranslationLimits2D copiedLimits = sliderJoint.limits;
// 2) Change what you need in the copy.
copiedLimits.max = targetLength;
// 3) Assign it back
sliderJoint.limits = copiedLimits;
Option 2 would be something like this:
// 1) Make a new one.
JointTranslationLimits2D newLimits = new JointTranslationLimits2D();
// 2) Fill out all the fields.
newLimits.max = targetLength;
newLimits.min = 0.0f;
// 3) Assign it.
sliderJoint.limits = newLimits;
I personally like option 1 in most cases, since I only have to change what I need.