How to create a 3d grid of cubes

Hello. I am fairly new to unity3d, but have been coding with other programs for many years. I am not sure how to create a 3d grid of cubes (such as 101010).
What I want to do in the future is be able to make shapes out of the cubes by deleting some, or changing colors.
But for now, I don’t even know how to spawn the cubes 3d grid.

I have started out by creating for loops like this:

for(var z=0;z<8;z++) {

for(var y=0;y<8;y++) {

for(var x=0;x<8;x++) {

//spawn cubes here

}}}

I have to clarify about how I spawn the cubes. I don’t want to spawn thousands of different objects, because this will cause extreme lag. Instead,all I need to do is store each of the cubes data into a giant array.

Please help lead me in the right direction.

ps. I have looked all over the internet, and I have found examples if spawning a 3d grid of cubes. However, this is not what I need. I need this in “data” form, like an array. Then, hopefully, I can control what is rendering and prevent serious lag.

OK, how about a three-dimensional array of Vector3’s, that seems to be what you are after:

 var threeWay : Vector3[,,] = new Vector3[8,8,8];

for(var z=0;z<8;z++) {

for(var y=0;y<8;y++) {

for(var x=0;x<8;x++) {

threeWay[x,y,z] = Vector3(x,y,z);

}}}

kind of redundant, but you get the idea!

EDIT: actually this is very redundant, you may as well just have an array of booleans, or whatever suits your needs, such as true for the slots that should create a cube, false for empty…\

var threeWay : boolean[,,] = new boolean[8,8,8];

…etc

    threeWay[x,y,z] = true;

since you’re going to have to cycle back through it the same way anyway…

(the default cube is 1 unit in size, so x,y,z in this case would create the grid…)

EDIT AGAIN::::

here is an example project:

http://dl.dropbox.com/u/11203897/SampleCubeGrid.zip

play the scene, and you can click on cubes (in the game view) to turn them on or off… This is JUST FOR DEMO PURPOSES, so you can see how to save/retrieve the data in your array, hope this helps

Here is a bit of code that produces an array of game object both physically and makes an array to track them. If you click the mouse, a random game object is either shown or hidden. Attach this script to an empty game object. To use the code you have to select the object this script is attached to and drag either a scene game object or a prefab onto the goPrefab variable. That is the object that will be duplicated. The code positions object 1 unit apart, so the prefab or game object should be 1 unit in size or smaller.

var goPrefab : GameObject;
var cubeEdge = 4;  // number of cubes on an edge
var v3Center = Vector3.zero;

private var argo : GameObject[,,] = new GameObject[cubeEdge, cubeEdge, cubeEdge];

function Start() {
	for (var i = 0; i < cubeEdge; i++) {
		for (var j = 0; j < cubeEdge; j++) {
			for (var k = 0; k < cubeEdge; k++) {
				var x = v3Center.x - cubeEdge / 2.0 + i;
				var y = v3Center.y + cubeEdge / 2.0 - j;
				var z = v3Center.z - cubeEdge / 2.0 + k;
				
				argo[i,j,k] = Instantiate(goPrefab, Vector3(x,y,z), Quaternion.identity);
			}
		}
	}
}

function Update()
{
  if (Input.GetMouseButtonDown(0)) {
  	var go : GameObject = argo[Random.Range(0, cubeEdge), Random.Range(0, cubeEdge), Random.Range(0, cubeEdge)];
  	go.renderer.enabled = !go.renderer.enabled;
  }
}