Block ground generation

I have written a very simple script to make a ground of blocks.

var object : GameObject;

function Start () {
	for(var x = 0;x < 100;x++)
	{
		for(var y = 0;y < 100;y++)
		{
			var cube = Instantiate(object,Vector3(x,y,0),Quaternion.Identity);
		}
	}
}

The object variable is the prefab that it should make the ground out of. In this case a cube.
But when i run it there is no ground. And I am at 0,0,0 position in the world.

This would not create a ground. It would create a wall.

2 Answers

2

It seems you’re building a wall. x is left, y is up and z is forward. so you want to create them along the x/z axes. Also object is a reserved keyword in C#, I don’t know how this translates to JS… But try renaming the object variable.

var prefab : GameObject;  // Renamed, because of uncertainty
var height : float = -1.0f; // Adjust this to find a proper height...

function Start () {
    for(var x = 0; x < 100; x++)
        for(var z = 0; z < 100; z++)
            Instantiate(prefab, Vector3(x, height, z), Quaternion.Identity);
}

Well this is odd. Put a print("Hello world") inside Start to see if it runs at all. Is your script enabled?

Are you sure you renamed the variable in Instatiate too? Are you sure your prefab is actually a prefab from the project and not something in your scene?

It turned out that I used a capital I in Quaternion.identity ! Damn case sensitive!

And you got a null reference exception for that? JS doesn't fail to surprise me :)