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

How to disable or destroy HingeJoint2D

Discussion in 'Scripting' started by Nadan, Apr 5, 2014.

  1. Nadan

    Nadan

    Joined:
    Jan 20, 2013
    Posts:
    341
    I have a lamp that is hanging from the ceiling in my 2D game. It is connected with HingeJoint2D.

    Somehow I need to disable the HingeJoint2D component, so the lamp will fall down. This works when I press the "enabled" checkbox in the inspector while testing the game.

    But I can't get the code to work. I've tried:

    Code (csharp):
    1. HingeJoint2D.enabled = false;
    and

    Code (csharp):
    1. Destroy(HingeJoint2D);
     
  2. Flavelius

    Flavelius

    Joined:
    Jul 8, 2012
    Posts:
    925
    and "HingeJoint2D" is your variable pointing to the component?
     
  3. sebrobitaille

    sebrobitaille

    Joined:
    Mar 9, 2013
    Posts:
    65
    Try:
    Code (csharp):
    1. Destroy(gameObject.getComponent("HingeJoint2d"))
     
  4. Nadan

    Nadan

    Joined:
    Jan 20, 2013
    Posts:
    341
    I tried:

    Code (csharp):
    1. var joint : HingeJoint2D;
    2. joint.enabled = false;
    But Unity says:

    UnassignedReferenceException: The variable joint of 'Jointti' has not been assigned.
    You probably need to assign the joint variable of the Jointti script in the inspector.

    And I don't know what this means. :)

    At first Unity didn't react at all to this, no error message so I didn't know that it works. Then after I changed "2d" to "2D" this worked.

    Code (csharp):
    1. Destroy(gameObject.GetComponent("HingeJoint2D"));
    I quess better way would be just to disable the HingeJoint2D, but Destroy does the trick.

    Thank you for the help!