How would I check if 2 objects are connected with fixed joints?

So the problem that i’m trying to solve is this.

2858412--209105--upload_2016-11-21_1-0-16.png

The red squares are GameObjects with BoxColliders2d and the green arrows are Fixed Joints2d.

So what I want to figure out is, if A and B are connected by a “chain” of joints.

Some other info that I should also prob tell is, that all joint are either upwards, or from the right to the left. ( as you can see in the pic)

Edit: So I figured out I should probably divide the squares into “grups”, however I have no idea how to do that…

Probably the best way to do this would be to have a class that manages each box, and also has the index of whatever group it’s a part of. If the box is already in a group, then it should ask a single group managing class to merge the two. Then checking if box A was in the same group as box B would be as simple as:

    bool isInSameGroup(JointManager boxA, JointManager boxB)
    {
        return (boxA.group == boxB.group);
    }

You could also just have one class managing all the boxes, assigning every box an index with a Dictionary or something.

How are you making the connections right now? If it’s within the Unity editor then you’ll need to make a mechanic during runtime to connect the boxes. At the moment, a method to find if A is connected to B is incredibly inefficient because you’ll have to loop through every box, add it to a list if it’s connected to anything in that list, and then return true once you get to B, or false if you never reach B. A very bad solution if you want to call this during Update() for example.