Hingejoint not responding on script

I have a hingejoint setup. When I press a key I can see the number at the “target position” in the inspector change but the mesh it is attached to does not move. But when I type in a random number at “target position” the mesh does move.

The script attached is very basic:

public float defaultPosition = 0.0f;
	public float targetPosition = 50.0f;
	public float springStrength = 100.0f;
	public float springDamper = 1.0f;

    private JointSpring spring;
	
	void Awake ()
	{
		GetComponent<HingeJoint>().useSpring = true;
		spring = new JointSpring();
		spring.spring = springStrength;
		spring.damper = springDamper;
	}
	
	void Update ()
	{    		
		if (Input.GetKeyDown (KeyCode.D)) 
		{
			Debug.Log ("D pressed");
			spring.targetPosition = targetPosition;
		}
		else
			spring.targetPosition = defaultPosition;
		
		GetComponent<HingeJoint>().spring = spring;
	}

-edit-
The problem is stranger then I thought. I removed the script component, added it again end it worked. But once I change a public variable in the inspector it breaks. Besides that if I re-attach the script component it does not always work, sometimes I have to re-attach it several times.

Instead of creating a new Spring instance here, you need to get the current spring from the HingeJoint. Something like:

 void Awake ()
 {
     GetComponent<HingeJoint>().useSpring = true;
     // Get the spring from the HingeJoing
     spring = GetComponent<HingeJoint>().spring;
     spring.spring = springStrength;
     spring.damper = springDamper;
 }

This appears to be a bug, I’ve created a github repository that illustrates the issue

A very simple scene, when you walk into the collider, the spring should open the door, and it does not - the values change in the project, but there is no movement. Manually changing the values while it is running works.

How do I file a bug report, and do they pay bounties?

I’ve also encountered this and devised a workaround. I’m not sure, but it seems like changing the constraints of a joint do not cause the physics engine to calculate a solution for the scene. By “prodding” the object whose joint’s constraints have been changed, you can force a solution. For me, after changing the constraints of a joint, I called AddForce(Vector3.zero) on the object’s rigidbody. This caused the new constraints to be enforced.

I’ve had the same problem and fixed it by calling

GetComponent().WakeUp();

After you change the spring values.