Help with a Cubeland-type script.

I finally got the script, I just want to thank jesse anders for giving me the unity documentation to help me figure the script out sorry for posting a lot. Anyways to the script when I put the script on my cube prefab and try to place the cube, it just goes crazy. What I mean is it will stack 20 cubes high from the ground. I don't want that, I want to stack one cube at a time. When I place a cube manually, I should be able to stack manually and stacking nice and neat.

This is not in editor mode this is in play mode and its in first person shooter. I probably confused you guys sorry about that hahahaha. If you guys still don't get what I'm trying, to say watch this video and you will see the player can stack the cubes nice and neat and the player can build like a castle or a house - thats what I want for my game. and stacking beside each other. Cubelands video

Heres the script:

function Start()
{
    for(i=0; i<20; i++)
    {
        var cube : GameObject = GameObject.CreatePrimitive(PrimitiveType.Cube);
        cube.transform.position = transform.position + Vector3(0, i, 0);
    }
}

Put this on an empty game object to generate floor:

for (var y = -4; y < 5; ++y)
for (var x = -4; x < 5; ++x) {
    var cube : GameObject = GameObject.CreatePrimitive(PrimitiveType.Cube);
    cube.transform.position = transform.position + Vector3(x, 0, y);
}

Put this on your camera to build or erase cubes:

var blockLayer : LayerMask = 1;
var range : float = Mathf.Infinity;
var hit : RaycastHit;

function Update () {
    if (Input.GetMouseButtonDown(0))
        Build();
    if (Input.GetMouseButtonDown(1))
        Erase();
}

function Build() {
    if (HitBlock()) {
        var cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
        cube.transform.position = hit.transform.position + hit.normal;
    }
}

function Erase() {
    if (HitBlock())
        Destroy(hit.transform.gameObject);
}

function HitBlock() : boolean {
    return Physics.Raycast(transform.position, transform.forward, hit, range, blockLayer);
}

Or a more compact version that does the same as the two above, but creates ground 3 units below player:

var range : float = Mathf.Infinity;
var hit : RaycastHit;

for (var y = -4; y < 5; ++y)
for (var x = -4; x < 5; ++x) {
    var cube : GameObject = GameObject.CreatePrimitive(PrimitiveType.Cube);
    cube.transform.position = transform.position + Vector3(x, -3, y);
}

function Update () {
    if (Input.GetMouseButtonDown(0) && Hit()) {
        var cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
        cube.transform.position = hit.transform.position + hit.normal;
    }
    if (Input.GetMouseButtonDown(1) && Hit())
        Destroy(hit.transform.gameObject);
}

function Hit() : boolean {
    return Physics.Raycast(transform.position, transform.forward, hit, range, 1);
}

Set your players layer to Ignore Raycast.

Hey man, can you be clearer in what you are looking for? Your question is not very well structured, try to clearly outline the characteristics of your block creation and interaction in bullet points to convey the idea.

The problem your encountering is due to the fact that your instancing code is in the Start() function. By placing this code in start it will instance all 20 blocks one after another (pretty much instantly) when the level starts. If these blocks have rigidbodies they'll fall about all over the place in the physics engine.

You need to take the instancing syntax and tie it to a function called by an input event. I'm not sure what your final goal is, if your shooting for something like Minecraft I suspect your going to need an array that keeps track of vector3 positions where objects exist or some alternative system of tracking block positioning vis a vis other blocks.