Multiple joints on GameObject

Hi Guys,

I am trying to add multiple joints to an object on collision. But it only Adds one joint to each object. So for example if there is a central sphere with 6 other neighbouring spheres the central sphere should have 6 joints connected to it.

This is the bit of code that I have. It seems to be creating the remaining 5 joints, but in the inspector it says that no rigid body is attached to the created joint. There is only 1 joint that displays a connected rigid body.

Any help would be great!, thanks

 private void OnCollisionEnter(Collision collision)
        {

                if (AP.addSpringJoints)
                {
                    
                    AddFixedJoint(collision);
                }


        }

 
        private void AddFixedJoint(Collision data)
        {
         
                gameObject.AddComponent<FixedJoint>();
                gameObject.GetComponent<FixedJoint>().connectedBody = data.rigidbody;
                gameObject.GetComponent<FixedJoint>().breakForce = Mathf.Infinity;
               

            }

GetComponent will always return the first FixedJoint you added.
Try this :

private void AddFixedJoint(Collision data)
{
    FixedJoint joint = gameObject.AddComponent<FixedJoint>();
    joint.connectedBody = data.rigidbody;
    joint.breakForce = Mathf.Infinity;
}