Clone an object and give it a unique position and name

Hi

Ive been stuck on this for a while now, i have searched the web and forums and cant find an answer :frowning:
When i use the code i found below it will put 1 extra cube into position but then adds like 50 GameObjects to my Hierarchy and i dont even use a loop?? btw i use javascript.

function Start () {
var original:GameObject = GameObject.Find("Cube");

//for(var i = 1;i<=5){
var cube:GameObject = Instantiate(original);
cube.transform.Translate(new Vector3(i,0,0));
//	}
}

when i try to use the loop unity just crashes.
does anyone know what im doing wrong or have a better way of achieving this?

im attempting to make a flat grid of cubes, maybe 10 by 10 for now, where i can identify one by clicking on it. but trying to get the generation of cubes working first

 for(l_row=0;l_row<10;l_row++)
{
for(l_col=0;l_col<10;l_col++)
{
GameObject.Instantiate(prefab, new vector(l_row, 0, l_col), Quaternion.Identity);
}
}

The above code will work, if you want to start spawning from X = 0, Z =0. with scale = 1( 1 unity unit)

Welcome to the forum. You have an endless loop. Try that:

for(var i = 1;i<=5; i=i+1)

Better is “i++”

Also, looks like your script is a Monobehaviour. If yes, it maybe better not to look for the original “Cube” but define your original as a public property and drop the prefab of your game object in Developer. This way you don’t have to think about your code if you want to switch to a different prefab.

I knew that someone would write that :slight_smile: