How to make a 40x40 map out of cubes, using script

I want to make a map out of single cubes (not just one big cube where i change the scale)
This is because i want to make an interactive map.

Someone told me i could do this with script and prefabs to make the map.

Now my question is, how do you do this? I can make the prefabs my own, but i’m for sure not experienced enough to make my own script, although i’m still learning and by every script i examine, it gets me further in understanding the scripts.

NOTE : Im using Javascript.

I used this script first but the problem with this one is that it will just generate random objects in a certain Min and Max coordinate i had given. this brings me problems like : holes in the floor and several objects that are standing in the same coordinates

var myCube : GameObject;
 
function Start()
{   
// Let's store some minimum and maximum possible x,y and z values
     // to position our cubes:
     var minX : int = -20;
     var maxX : int = 20;
     var minY : int = 1;
     var maxY : int = 1;
     var minZ : int = -20;
     var maxZ : int = 20;
     // (you can fiddle with these numbers to change the range of possible spawn positions)
 
     var totalCubes : int = 500; // Change this to whatever number of cubes you'd like to have on-screen
 
     var rot : Quaternion = Quaternion.identity;
 
     for(i=0; i<totalCubes; i++)
     {
          // Use Random.Range to grab randomized x,y and z values within our min/max ranges:
          var randomX : int = Random.Range(minX, maxX);
          var randomY : int = Random.Range(minY, maxY);
          var randomZ : int = Random.Range(minZ, maxZ);
 
          var pos : Vector3 = new Vector3(randomX, randomY, randomZ);
 
          Instantiate(myCube, pos, rot); // Put a new cube on the screen using a randomized position   
     }

}
 
function Update()
{
}

Then i used this script but it would just take to long to set all the blocks, and because i wanted to make a map out of 40x40 cubes that means i had to set 1600 blocks manually.

var myCube : GameObject;
 
function Start()
{
     // Store a bunch of different positions in an Array:
     var aPositions : Array = [new Vector3(0,1,0),
                                      new Vector3(1,1,0),
                                      new Vector3(2,1,0),
                                      new Vector3(3,1,0),
                                      new Vector3(4,1,0)];
 
     var rot : Quaternion = Quaternion.identity;
 
     // Loop as many times as there are elements in the aPositions Array:
     for(i=0; i<aPositions.length; i++)
     {
          Instantiate(myCube, aPositions*, rot); // Put a new cube on the screen using the i'th position* 

}
}
I can use every kind of help :slight_smile: - Hexer.

You almost had the right idea in the first script. Let’s look at the requirements :

  • a 2D grid of cubes
  • every position in the grid must have a cube
  • the height is different for each position in the grid

The way would be to loop through every x position, and within that every z position. Please read all the comments in my example script :

var myCube : GameObject;

var minX : int = -20;
var maxX : int = 20;
var minY : int = 0;
var maxY : int = 4;
var minZ : int = -20;
var maxZ : int = 20;

function Start()
{
	// loop for every z position in the grid
	for ( var z : int = minZ; z < maxZ; z ++ )
	{
		// now loop for every x position in the grid
		for ( var x : int = minX; x < maxX; x ++ )
		{
			// get a random value for Y
			var randomY : int = Random.Range( minY, maxY );
			
			// put it all to together to assign a position
			var pos : Vector3 = new Vector3( x, randomY, z );
			
			// instantiate the cube into a variable, so you can do other things with it
			var clone : GameObject = Instantiate( myCube, pos, Quaternion.identity );
			
			// change the name of the object, include the x and z position in the name
			clone.name = "Cube_" + x.ToString() + "_" + z.ToString();
			
			// in future, 
			// this would be stored in a 2D array for future reference
			// so you could modify the position, material, anything!
			// cubeArray[x,z] = clone;
		}
	}
}