- Home /
Creating a 2d array of gameObjects
Hi there,
I know this question has been asked a few time before but I can't find an efficient solution to my problem. I want to create a 2.5D isometric game in Unity. I need to create a grid of gameObjects that will be my level.
At the moment I have this code.
var blockPiece1:GameObject; var blockPiece2:GameObject;
var levelArray:Array = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0,0,0, 0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0, 0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0, 0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0, 0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0, 0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0, 0,0,0,0,0,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0, 0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
function Awake() {
for(x=0; x<20; x++){
for(z=0; z<20; z++){
print("x: " + x + " z: " + z + " type: " + levelArray[z*20 + x]);
if(levelArray[z*20 + x] == 1){
newPiece = Instantiate (blockPiece1, Vector3(x,1,z), Quaternion.identity);
} else {
newPiece2 = Instantiate (blockPiece2, Vector3(x,1,z), Quaternion.identity);
}
}
}
}
This works but seems to be a very inefficient way of creating the Array. I need to be able to click objects and change their type within some kind of Array.
There's rarely a reason to use Array; use int[] ins$$anonymous$$d, then your code will be much faster and better.
Answer by aldonaletto · Aug 21, 2011 at 04:13 PM
You can create a bidimensional GameObject array and fill it when creating your pieces. Later, at Update, you can access each object using this array - something like obj = pieces[row,col];:
EDITED: Included @Eric5h5 suggestion - changed levelArray to an int array - it's much more efficient than the Array class, which is untyped and requires a lot of system resources (memory and CPU time). It makes negligible difference in this script, but may serve as an useful example in other cases.
var blockPiece1:GameObject; var blockPiece2:GameObject; var levelArray:int[] = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0,0,0, 0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0, 0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0, 0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0, 0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0, 0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0, 0,0,0,0,0,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0, 0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]; var width = 20; var length = 20; var pieces: GameObject[ , ];
function Awake() { pieces = new GameObject[width, length]; // creates the object array for(x=0; x<width; x++){ for(z=0; z<length; z++){ print("x: " + x + " z: " + z + " type: " + levelArray[z*20 + x]); var prefab: GameObject; if(levelArray[z*20 + x] == 1) prefab = blockPiece1; else prefab = blockPiece2; pieces[x,z] = Instantiate (prefab, Vector3(x,1,z), Quaternion.identity); // you can also add a significant name to each piece: pieces[x,z].name = "Piece"+x.ToString("D2")+z.ToString("D2"); } } }
Hey,
Thanks for that. Is there a better way to deter$$anonymous$$e what block gets placed. I will have a giant if statement with all my block types at the moment.
Thanks
Will
I think what you want is an array of prefabs. That way you can simply use the level array number as a lookup into your prefab array. In c# I know it's relatively easy to make a public array of prefabs, but I'm not certain on how to do it in Java.
@Doddler: Unity doesn't use Java. In Javascript, it's basically the same.
I did that, but it tells me that transform[,] cannot be passed to transform. It's like the instantiate object is'nt a transform[,]
Answer by NE07HEKIN6 · Sep 02, 2011 at 07:35 PM
Sorry, i was trying to use this script, but im not able to reformulate this to add more prefabs, i try a few way but i can´t figure it the right one. If anyone knows how i will apreciate a lot your help.
If you have several prefabs, it's better to create a GameObject array and fill it in the Inspector, then select the desired one with a switch statement, like this:
var blockPiece: GameObject[]; // fill this array in the Inspector
... var prefab: GameObject; switch (levelArray[z*20 + x]){ case 1: prefab = blockPiece[0]; break; case 2: prefab = blockPiece[1]; break; case 3: prefab = blockPiece[2]; break; case 4: prefab = blockPiece[3]; break; } pieces[x,z] = Instantiate (prefab, Vector3(x,1,z), Quaternion.identity); ... But there's an even easier way: ins$$anonymous$$d of the switch statement, use levelArray[] directly as the blockPiece index:
... var prefab: GameObject; prefab = blockPiece[levelArray[z*20 + x]]; pieces[x,z] = Instantiate (prefab, Vector3(x,1,z), Quaternion.identity); ...
Thats it!! Thats what i was looking for! Great man, thanks for your help.
Your answer
Follow this Question
Related Questions
Checking if a position is occupied in 2D? 1 Answer
How to get adjcent tiles (horizontal or vertical) in an isometric tilemap ? 1 Answer
Having trouble iterating through an array of GameObjects of a certain type 1 Answer
How do you get the tile on which a gameObject is standing on Isometric Tilemap ? 1 Answer