# in Unity3d, how to automatically create fixed joints in a wall of cubes?

I want to create a wall:
width 6 cubes
height 7 cubes
therefore I have 22 cubes on the margins and 20 cubes inside. For each of the margins cubes(except the corners) I have to set 3 fixed joints, and for the cubes inside the wall I have to set 4 fixed joints - all this to have good physics behavior.
Pretty much I have to create a fixed joint between a cube and all his neighbors => and that's a lot!
Is there a way to automatically create these fixed joints?

Is there another way to create a physics friendly wall? I want to simulate a wall of bricks hited by a projectile.

-

I usually don't write code for these questions, but I'm bored. I don't have unity on this computer so I'm just guessing, it should give you an idea even if it doesn't compile. I just added each blocks right and top joint as long as it's not on the top row or far right column. In a real situation you would add the parent transforms x and y position and do all positioning in local space so it can accept the parents transform, but you get the idea I hope.

``````int x = 6;
int y = 7;
float hSpacing = 0.5f;
float vSpacing = 0.5f;
GameObject[,] wall = new GameObject[x,y];

for(int i = 0;i<width;i++)
{
for(int j = 0;j<height;j++)
{
wall[i,j] = (GameObject)Instantiate(cube,
new Vector3(i*(cube.lossyScale.x+hSpacing),
j*(cube.lossyScale.y+vSpacing),0);

if(i+1<width)
{
FixedJoint fjR = wall[i,j].AddComponent("FixedJoint") as FixedJoint;
fjR.connectedBody = wall[i+1,j].rigidbody;
}