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.

In these cases you want to use in-editor scripting to quickly set things up.

I would use one script to actually create them all and a separate one to tweak them, so you have more freedom when tweaking and so they are all created when referencing eachother. Firstly create an empty GameObject named "Wall", place it where you want the left-most brick to be and simply run:

@script ExecuteInEditMode()

var done : boolean = false;
var wall : GameObject;
var width : int = 6;
var height: int = 7;

var cube : GameObject;
var cubeWidth : float;
var cubeHeight : float;

function Update ()
{
    if (!done)
    {
        for (var i = 0; i < height; i++)
        {
            for (var j = 0; j < width; j++)
            {
                var newCube = Instantiate (cube, Vector3(wall.transform.position.x+j*cubeWidth, wall.transform.position.y+i*cubeHeight ,wall.transform.position.z), Quaternion.identity);
                newCube.name = ""+i+"-"+j; //names them after their position in the grid for easy refferencing;
            }
        }

    done = true;
    }
}

next you want a script that sets everything up:

@script ExecuteInEditMode()

var done : boolean = false;
var wall : GameObject;

var height : int = 7;
var width : int = 6;

function Update ()
{
    if (!done)
    {
        var cubes : Array = wall.GetComponentsInChildren(Transform);

        cubes.Shift(); //remove the wall itself from the array

        for (var i = 0; i < cubes.length; i++)
        {
            cubes*.gameObject.AddComponent(Rigidbody);*
 *if(i < (cubes.length-width)) //connect each to the one above it, so exclude the upper row.* 
 *{* 
 _cubes*.gameObject.AddComponent(HingeJoint);*_
 <em>_cubes*.gameObject.hingeJoint.connectedBody = cubes[i+width].gameObject.rigidBody;*_</em>
 <em>_*}*_</em>
 <em>_*}*_</em>
 <em>_*done = true;*_</em>
 <em>_*}*_</em>
<em>_*}*_</em>
<em>_*```*_</em>
<em>_*<p>Now it's up to you to edit this script that it does not only attach it to each one above it, but also below, right and left to it. Good luck!</p>*_</em>

Hi I do not understand the code you gave why it does not work
I got this message: Script newbehaviourscript has not finished compilation yet. Please wait until compilation of the script has finished and try again.