How to connect two objects on collision?

I want to connect two game objects when they collide with each other. I tried doing this with distance joint 2D (Attaching it to the object that collides into the other object and trying to connect them with “.connectedBody” using “OnCollisionEnter” function), but no matter how I tried to write the script I couldn’t figure it out. Any other ideas? If possible a short example with a script.

You can do this by making one object child of other.
childobject.transform.parent = parentObject.transform;