HingeJoint Motor's values not being changed by script (show in debug, but not inspector)

Hi everyone,

I am working on a script where the player’s triggers turn a motorized hinge in two directions, and everything is working smoothly if I manually edit TargetVelocity in the inspector window. However, if I edit the values using c# the changes are not being reflected in the game, but still show in the console as intended.

Here’s a picture of the inspector, showing my most of my hinge settings, and what I’m seeing in the editor:

Here is my script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class caneTorque : MonoBehaviour
{
    //Input Setup
    public float leftTrigger;
    public float rightTrigger;
    public float stickSensitivity;
    public float leftRightBalance;
    //Physics
    public Rigidbody rb;
    public HingeJoint caneHinge;
    public JointMotor caneHingeMotor;

    void Start()
    {
        rb = GetComponent<Rigidbody>();
        caneHinge = GetComponent<HingeJoint>();
        caneHingeMotor = caneHinge.motor;
    }

    void Update()
    {
        //Getting controller input and getting desired direction
        leftTrigger = Input.GetAxis("LTrigger");
        rightTrigger = Input.GetAxis("RTrigger");
        leftRightBalance = ((-leftTrigger) + rightTrigger) * stickSensitivity;
    }
    void FixedUpdate()
    {
        //Setting target velocity
        caneHingeMotor.targetVelocity = -leftRightBalance;
        //Attempt to refresh the physics suggested by @kinggryan 
        rb.AddForce(Vector3.zero);
        //In console this will work as expected, but inspector & game does not update
        print(caneHingeMotor.targetVelocity);
    }
}

I found This question where @kinggryan suggested applying force to the rigidbody in order to
force Unity to go to work on applying the new values. Unfortunately in my case, it does not seem to have worked, so I thought it best to admit defeat and ask for any insight or ideas on how to solve this puzzle. I’m on Unity 2017.3.1f1 in case it’s any help.

Thank you so much for your time,

Adam

In order to change the values, you actually have to assign the motor member like this:

 void FixedUpdate()
 {
     //Setting target velocity
     caneHingeMotor.targetVelocity = -leftRightBalance;

     caneHinge.motor = caneHingeMotor;

 }

The motor property doesn’t “gather” the values you change until the motor property member is assigned.

Also, make sure you assign a force to the motor (caneHingeMotor.force), or it still won’t move.