- Home /
Hinge Joint Limits in C sharp
I'm making a pinball demo. I get an error that says error CS1612: Cannot modify a value type return value of `UnityEngine.HingeJoint.limits'. Consider storing the value in a temporary variable. My errors are the lines that say hingeJoint.limits.min & hingeJoint.limits.max. I would be grateful for any help. Thank you for your time and have a nice day.
using UnityEngine; using System.Collections;
public class Flippers : MonoBehaviour {
float startPosition = 0f;
float buttonPosition = 45f;
float flipperStrength = 10000f;
float flipperTorque = 25f;
string flipperName = "LeftFlipper";
void Awake () {
hingeJoint.useSpring = true;
}
// Update is called once per frame
void Update () {
JointSpring spring = new JointSpring();
spring.spring = flipperStrength;
spring.damper = flipperTorque;
if (Input.GetButton(flipperName))
spring.targetPosition = buttonPosition;
else
spring.targetPosition = startPosition;
JointSpring s = new JointSpring();
hingeJoint.spring = spring;
hingeJoint.useLimits = true;
hingeJoint.limits.min = startPosition; // Here's my error
hingeJoint.limits.max = buttonPosition; // Here's my error
}
}
Answer by Owen-Reynolds · Jul 27, 2014 at 05:02 PM
It's the same error as when you try to say transform.position.x=6;
. You can only assign an entire limits variable. Ex from memory:
JointLimits jj = hingeJoint.limits;
jj.min= 3; jj.max=4;
hingeJoint.limits = jj;
The only reason for the equals in the first line is to keep the bounce variables that you already had.
There's nothing especially virtuous about doing it this way. It's more of getting around a design flaw in java/C#. If everything was just fields, you could always say dog..fur.color.r=1. But whenever something is turned into a Property, you aren't allowed to "reach through" and assign to parts. So you have to copy to a temp, change it, then copy the whole thing back.