Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

hingeJoint.useMotor (script) not working at all on pinball flipper.

Discussion in '2D' started by six5hot, Nov 6, 2014.

  1. six5hot

    six5hot

    Joined:
    Nov 6, 2014
    Posts:
    10
    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;
    }
    }
     
  2. six5hot

    six5hot

    Joined:
    Nov 6, 2014
    Posts:
    10
    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?





     
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,414
    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<HingeJoint2D>();' (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:

    'hingeJoint.useMotor = Input.GetKey ("o")
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,414
    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'.
     
  5. six5hot

    six5hot

    Joined:
    Nov 6, 2014
    Posts:
    10
    Ya I modified it to the below code and it worked.

    if (Input.GetKey("o")) {
    gameObject.GetComponent(HingeJoint2D).useMotor = true;
    }else{
    gameObject.GetComponent(HingeJoint2D).useMotor = false;
    }
     
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,414
    Cool.

    I would recommend that you set the reference to the HingeJoint2D to a variable rather than continuously looking it up like so:

    var hinge : HingeJoint2D;

    function Start ()
    {
    hinge = GetComponent (HingeJoint2D);
    }

    function Update ()
    {
    hinge.useMotor = Input.GetKey("o");
    }


    Even better, use C# instead :)
     
  7. six5hot

    six5hot

    Joined:
    Nov 6, 2014
    Posts:
    10
    Ya I've been tweaking it and its working nicely. The 2d pinball thing is actually working pretty good.

    So you think I'm better off using c# vs JavaScript in general for Unity?
     
  8. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,414
    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.
     
  9. six5hot

    six5hot

    Joined:
    Nov 6, 2014
    Posts:
    10
    Thats good enough for me
     
  10. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,414
    Have fun!