# Adding fixedJoint 2D at run time

**URL:** <https://forums.kodeco.com/t/adding-fixedjoint-2d-at-run-time/36274>\
**Category:** Game Tech\
**Created:** [November 5, 2017, 8:41pm UTC](https://forums.kodeco.com/t/adding-fixedjoint-2d-at-run-time/36274 "2017-11-05T20:41:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![skywignedace](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@skywignedace](https://forums.kodeco.com/u/skywignedace)\
**Post date:** [November 5, 2017, 8:41pm UTC](https://forums.kodeco.com/t/adding-fixedjoint-2d-at-run-time/36274/1 "2017-11-05T20:41:57Z")

</div>

I’m looking to add a fixed joint to an object when it collides with an object.

the idea is a ball touching this object and a fixed joint attaching it to the object.  
I chose to do it this way so that physics handles the ball and I don’t have to set it as a the child of the object it came in to contact with.

Could someone help me understand how to instanciate fixedJoint2D and connect it to an unrelated object through script?

---

<div class="post-metadata">

**Author:** ![blackdragonbe](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/blackdragonbe/32/109865_2.png) [@blackdragonbe](https://forums.kodeco.com/u/blackdragonbe)\
**Post date:** [November 5, 2017, 11:57pm UTC](https://forums.kodeco.com/t/adding-fixedjoint-2d-at-run-time/36274/2 "2017-11-05T23:57:03Z")

</div>

@skywignedace

You can add a FixedJoint2D component to an object and make it attach to a Rigidbody2D like this:

```
FixedJoint2D fj = gameObject.AddComponent<FixedJoint2D >() as FixedJoint2D;
fj.connectedBody = rigidbody2DOfOtherObject;

```

You can add that in OnCollisionEnter2D() for example to attach it on impact.

Cheers!  
Eric

---

<div class="post-metadata">

**Author:** ![system](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@system](https://forums.kodeco.com/u/system)\
**Post date:** [April 21, 2018, 12:41pm UTC](https://forums.kodeco.com/t/adding-fixedjoint-2d-at-run-time/36274/3 "2018-04-21T12:41:59Z")

</div>

This topic was automatically closed after 166 days. New replies are no longer allowed.
