Store Joint Motor as temporary variable?

Hi all. I’m trying to access the motor properties of a hinge joint. I assign a variable to access the joint in the first place, but when I try joint.motor, i’m not allowed to change values. I want to be able to change the target velocity of the motor property at runtime.

HingeJoint joint;

void Awake()
{
    joint.motor.targetVelocity = 10.0f;
}

Is giving me error:

Assets/example.cs(13,23): error CS1612: Cannot modify a value type return value of `UnityEngine.HingeJoint.motor'. Consider storing the value in a temporary variable

First off, you need to define your joint variable in awake before you can access its properties, or you’ll get a null reference exception at runtime. Unless you have this as a public HingeJoint that you can assign in the inspector. Then you need to use a JointMotor as that temporary variable for your motor property.

using UnityEngine;
using System.Collections;

public class example : MonoBehaviour
{
	HingeJoint joint;
	JointMotor motor;
	
	void Awake()
	{
		joint = GetComponent<HingeJoint>();
		motor = joint.motor;
		motor.targetVelocity = 10.0f;
	}
}

There are plenty of good references from googling this: Google