How to configure a "Connected Rigid Body" in a DistanceJoint2D inside a C# script?

I am trying to make a swing mechanism which could swing around a fixed pivot (a GameOject) which has a kinematic rigidbody. The objective is to use DistanceJoint2D for the swing mechanism. But the problem I am facing is to Set and Change the “Connected Rigid Body” inside the script. Is there a way out for it. If yes, Please help me through this. Thank You :slight_smile:

As shown in the screenshot, the anchor is not set on the desired GameObject. Is there any possible solution than i would highly appreciate your guidance. Thank You.!

Hi, if you want to dynamically assign the rigid body to the DistanceJoint2D, the field is called “connectedBody” not “attachedRigidBody” like you might think.

myGameObj.GetComponent<DistanceJoint2D>().connectedBody = otherObj.GetComponent<Rigidbody2D>();