how to increment an instantiated variable's name and then stick it in either y or x array?

var tilePrefab : Transform;
var FloorArray = new Array();
var nameN : int = 0;

var gridY = 10;
var gridX = 10;
function Start () {

// if you change the SIZE of the TILEFLOOR PREFAB remember to change the Instantiate sizes

 for(var x : int = 0; x < gridX; x++){
 		
 			for( var y : int = 0; y < gridY; y++){
 				
 				Instantiate(tilePrefab, Vector3(x * .5,0, y * .5) ,Quaternion.identity);
 					
 					FloorArray.Add(tilePrefab.name + nameN);
			
			print(FloorArray);
			
		}
	}
}

ok so i am trying to make it so that the tile prefab increments its number for each clone it makes… but it isn’t coming out good.
then i want to create a multidimensional array and put the y’s in the second spot and the x’s in the first slot? how can i do this?

3 Answers

3

Do something like this:

var newGO = Instantiate(tilePrefab, Vector3(x * .5,0, y * .5) ,Quaternion.identity);
newGO.name = tilePrefab.name + "_"+x+"_"+y;
FloorArray.Add(tilePrefab.name + newGO.name);

But you could also just have a number you increment like

var i = 0;
...
...

    var newGO = Instantiate(tilePrefab, Vector3(x * .5,0, y * .5) ,Quaternion.identity);
    newGO.name = tilePrefab.name + "_"+i;
    i++;
    FloorArray.Add(tilePrefab.name + newGO.name);

And look into two and multidemensional arrays for storing them.

In order to name each instance created with “something” + nameN you must get a reference to the new object and change its name:

...
    var tile:Transform = Instantiate(tilePrefab, Vector3(x * .5,0, y * .5) ,Quaternion.identity);
    tile.name = "Tile"+nameN.ToString();
...

I didn’t get what you want to do with the multidimensional array. Can you give some example?

basically what i want for the arrays is to create an x and y grid pattern so bottom left is tile (0,0) or tile_x0_y0 and then the tile to the right of that is (0,1) or tile_x0_y1. i am then going to have all about 20 tiles of each and create a basic grid so that i can tell my characters where i want them to go.

To use multi-dimensional arrays:

var floorArray : GameObject[,];
...
floorArray = new GameObject[gridX,gridY];
for(var x : int = 0; x < gridX; x++){
        for( var y : int = 0; y < gridY; y++){
                floorArray[x,y] = Instantiate(tilePrefab, Vector3(x * .5,0, y * .5) ,Quaternion.identity) as GameObject;
        }
}

so i wasn't to far off, thats nice to know. lol i am getting somewhere!

so if i get this right the part where its floorArray = new GameObject [gridX,gridY]; that parts just defining how big the array is right? then at the bottom though i dont under stand how it knows which one of the x or the y to put it into in this part floorArray[x,y] = Instantiate(tilePrefab, Vector3(x * .5,0, y * .5) ,Quaternion.identity) as GameObject; does that make sense what i am asking? i just want to learn from it and not have my game be abunch of random code that i dont even understand my self.