I cannot connect bones together properly using a list. I'm baffled, could anyone please help?

Hello everyone!
So I’m trying to connect bones with a Spring joint using a script. so I don’t have to manually do it.
But for some reason I my script can only connect the elements that are before the bone I want to set the Joints for (or it can also connect it to itself).
It’s very weird, I cannot wrap my head around why it doesn’t work, hwere is the code:
[

        int counter = 0;
        Component[] bones2 = GetComponentsInChildren < Transform > ();
        List < Component > bones = bones2.ToList();
        bones.RemoveAt(0); //Removes the first element that is for some reason the parent

        foreach(Component bone in bones) {

          if (bone.transform.childCount == 0) {
            SpringJoint2D spring1 = bone.gameObject.AddComponent(typeof (SpringJoint2D)) as SpringJoint2D;
            SpringSetter(spring1);
            spring1.connectedBody = bones[counter - 1].GetComponent < Rigidbody2D > ();

          } else if (counter - 1 == -1) {
            SpringJoint2D spring1 = bone.gameObject.AddComponent(typeof (SpringJoint2D)) as SpringJoint2D;
            SpringSetter(spring1);
            spring1.connectedBody = bones[counter + 1].GetComponent < Rigidbody2D > ();

          } else {
            SpringJoint2D spring1 = bone.gameObject.AddComponent(typeof (SpringJoint2D)) as SpringJoint2D;
            SpringSetter(spring1);
            spring1.connectedBody = bones[counter - 1].GetComponent < Rigidbody2D > ();
            SpringJoint2D spring2 = bone.gameObject.AddComponent(typeof (SpringJoint2D)) as SpringJoint2D;
            SpringSetter(spring2);
            spring2.connectedBody = bones[(counter + 1)].GetComponent < Rigidbody2D > (); //This line refuses to work! It always keeps it null
            Debug.Log(bones[counter + 1].gameObject.name);
          }
counter++;
        }

        private static void SpringSetter(SpringJoint2D spring1) {
          spring1.frequency = 8 f;
          spring1.dampingRatio = 0.8 f;
        }

Any help is apprechiated. This script simply refuses to get the bones[counter+1] element

Trying to understand the point: 1st IF) if the bone has no children, connect it to the bone before it. 2nd IF) the first bone ( if(counter-1==-1) is just a funny way of saying if(counter==0)) connects to the one after it, else) otherwise connect to the ones before and after.

I don’t see what the “if it has no children” is for. Worse, if the first bone has no children it will crash (connecting to bone -1). The next part about bone 0 only connecting to bone+1, and not bone-1 which doesn’t exist, is standard “don’t go off the edge” stuff. But then you should check for the last bone so you don’t go off that edge with bone+1.

Beyond that stuff, you don’t need joints both ways. If bone2 connects to bone3, you don’t want bone3 to have another spring going back to bone2. Joints are 2-way (sure, Unity says they go on 1 thing and connect to another, but that’s only because of how Unity works – you can’t have one Spring component on two bones at the same time. But the rest of the system knows springs are really between two things and affect both).

1 Like

Thanks!
Yeah I knew about most of the problems, but yeah I guess only one joint is enough, thanks for the help! I don1t remeber why I have written counter-1 == -1 :smile: