I have a pinball flipper that I am trying to make rotate when I hit a key. If I tick on the usemotor in the UI box it does turn fine when I hit play but if I try to turn on the motor via script it will not work. Its odd, it should not be complicated to get the motor to activate via script not sure why it wont work.
Entire JS script is below, any ideas?
function Update() {
if (Input.GetKey(“o”)) {
hingeJoint.useMotor = true;
} else {
hingeJoint.useMotor = false;
}
}
An update, I am getting an error message that says
MissingComponentException: There is no ‘HingeJoint’ attached to the “left-flipper” game object, but a script is trying to access it.You probably need to add a HingeJoint to the game object “left-flipper”. Or your script needs to check if the component is attached before using it. LeftFlipper.Update () (at Assets/Scripts/LeftFlipper.js:11)
The thing is I do have a Hindge Joint 2D Component attached to the left-flipper game object . Why would it say I do not have it attached?
Not really enough information in your post to give you the solution to your problem, only things to check.
I presume ‘hingeJoint’ has been initialized with something like ‘HingeJoint2D hingeJoint = GetComponent();’ (c#)
Try checking what the motor is actually set to by reading the ‘motor’ property that returns a JointMotor2D structure. Not sure if you’re setting these at runtime or not or just leaving the values to what you set in the editor.
Just trying to help here but note you can simplify the above script using something like:
I just saw your post after I posted my reply. So yes, if that error is ‘exactly’ what is appearing then it looks like you’re using the 3D hinge joint as that is named ‘HingeJoint’ whereas the 2D one is ‘HingeJoint2D’.
The language choice is a personal preference. If you come from a JavaScript background then you’ll obviously feel more comfy using it however internally at Unity we’re using C# and it’s become the primary language of choice. Indeed, you’ll see all tutorials and documentation written using it.