- Home /
Instantiating Multiple Prefabs at Runtime
So I found some code to create a floor out of cube objects and I am trying to adapt this code to use GameObjects instead. I have created a Cube GameObject and added a texture to it, which is in my Project window. I dragged this Cube onto my Block prefab which was empty. Then I created an empty GameObject and placed it in the Hierarchy window. I added my Initialize script to this empty GameObject. Here is the code:
//Creates the floor of the world
var min = -25;
var max = 25;
var block : GameObject;
function start() {
for (var y = min; y < max; ++y) {
for (var x = min; x < max; ++x) {
var newBlock = Instantiate(block, Vector3(x,0,y), Quaternion.identity);
newBlock.transform.position = transform.position + Vector3(x, 0, y);
}
}
}
In the Hierarchy window I clicked on the GameObject with this script attached and assigned the block variable to my Block prefab. This code should make a 25x25 square of blocks, but instead none of the blocks are spawning. I'm pretty sure the code is right but I'm not sure why nothing is happening. Any help would be greatly appreciated.
Just making sure this is just a typo, your code is using Start() and not start() right? The capitalization is important.