How do I add a HingeJoint with spring from script?

I am updating a game made with unity 4 to unity 5.6 and am experiencing lots of problems with joints. Right now I’m trying to add and setup a HingeJoint (3d) from script, but I just can’t get it to work.

I have set up the simplest example I can think of, an elongated cube with a rigidbody and a box collider. I want to add the hinge joint at the bottom and give it some springyness so that when it is hit it gets back to upright position.

If I do it manually, it works fine, but if I add the hinge joint with AddComponent and put in the same values I get no spring. When hit, the cube gets slightly dislocated and then stay there or jitters away to some random position if it gets hit again.

Here are my two functions. They are called right after each other, no funny business. HingeAtGameobject is just a plain GameObject positioned where I want the joint to be. I’ve tried fixed values for the anchors too, but no luck. Help!

	private void CreateHinge()
    {
        _hingeJoint = _gameObject.AddComponent<HingeJoint>();
        _hingeJoint.axis = Vector3.forward;
        
        _hingeJoint.autoConfigureConnectedAnchor = false;
        _hingeJoint.anchor = hingeAtGameObject.localPosition;
        _hingeJoint.connectedAnchor = hingeAtGameObject.position;

        _hingeJoint.enableCollision = false;
        _hingeJoint.enablePreprocessing = false;        
    }

    private void SetupSpring()
    {
        _hingeJoint.useSpring = useSpring;
        if (useSpring)
        {
            JointSpring jointSpring = _hingeJoint.spring;
            jointSpring.spring = spring;
            jointSpring.damper = damper;
            jointSpring.targetPosition = 0f;
            _hingeJoint.spring = jointSpring;
        }
    }

After two days of sweating the problem is solved. The solution was in the rigidbody, I had constrained it to only rotate on z axis, but the hinge joint takes care of this when you set axis and breaks if the rigidbody also has constraints.

Also found that you had to be careful what order you set things up in the hinge joint. I ended up setting only the anchor, then the axis and then let the rest take care of itself:

private void CreateHinge()
    {
        _hingeJoint = _gameObject.AddComponent<HingeJoint>();
        _hingeJoint.anchor = hingeAtGameObject.localPosition;
        _hingeJoint.axis = Vector3.forward;
    }

    private void SetupSpring()
    {
        _hingeJoint.useSpring = useSpring;
        if (useSpring)
        {
            JointSpring jointSpring = _hingeJoint.spring;
            jointSpring.spring = spring;
            jointSpring.damper = damper;
            jointSpring.targetPosition = 0f;
            _hingeJoint.spring = jointSpring;
        }
    }