- Home /
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
Answer by clunk47 · Sep 23, 2013 at 08:15 PM
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: https://www.google.com/#q=unity+joint+motor
Well thanks for the answer, but not for telling me to google... I didn't know what to search for on google so yeah. Anyway thanks, the ANSWER part of this works for me :)
You say "I'm trying to access the motor properties of a hinge joint.", you also tag your question with "joint" and "motor". $$anonymous$$y google link is a perfect reference in this case. Google: Joint $$anonymous$$otor. $$anonymous$$akes sense no? Take suggestions on UA as advice, not as offensive remarks. I took the time to help you out with something you obviously needed help with. Every part of my ANSWER is my ANSWER. Glad I could be of help, happy developing~:)
I found that this code works in unity 4.6 only if you execute "joint.motor = motor;" after setting the motor's target velocity. Oh, and I got here by searching google for those exact terms :)
Your answer
Follow this Question
Related Questions
How can i change HingeJoint's axis from code? 0 Answers
Trying to use JointMotor, nothing happens 1 Answer
unity3d motorScript 0 Answers
Pushing a cart with a vehicle (Not generating initial torque) 0 Answers
HingeJoint Motor's values not being changed by script (show in debug, but not inspector) 1 Answer