- Home /
Assigning Instantiated Tiles to Array
Here is what I am trying to do (here at least). I want to create a playing board called World, and have it made up of separate cubes (which are actually planes, but whatever). I have one prefab thats a generic cube, then a couple cubePrefabs that are different types. At runtime, I want to board to be created, then set all the cubes into an array. Then, during Update functions I will (either destroy or place over the top) add the cubePrefabs to change the World. I have created the world, and the cubes, and they all get named, but I can't figure out how to set each named Cube into an Array that I can call on to change during update. I am assuming I am calling in the array function at the wrong time, but I am not sure where to stick it. Any help would be greatly appreciated.
public class MakeDaCubes : MonoBehaviour {
public int board_size_x_;
public int board_size_z_;
public Transform cube_prefab_;
public GameObject[] cubePrefabs;
public Transform[] boardCubes;
// Use this for initialization
void Start()
{
//Creates and instantiates the board
GameObject board = new GameObject();
//Names the board World
board.name = "World";
//math to make X variable grow to board_size_x
for (int x = 0; x < board_size_x_; x++)
{
//math to make Y variable grow to board_size_y
for (int z = 0; z < board_size_z_; z++)
{
Transform cube = (Instantiate(cube_prefab_, new Vector3(x, 0, z), Quaternion.identity) as Transform);
cube.name = "Cube" + x + "," + z;
cube.parent = board.transform;
Transform[] boardCubes = new Transform[]{cube};
}
}
}
New Code resulting in multiple debug logs being posted:
public class MakeDaCube : MonoBehaviour {
public int board_size_x_;
public int board_size_z_;
public Transform cube_prefab_;
public GameObject[] cubePrefabs;
public Transform[] boardCubes;
private int b;
// Use this for initialization
void Start()
{
Transform[] boardCubes = new Transform[(board_size_x_ * board_size_z_)];
//Creates and instantiates the board
GameObject board = new GameObject();
//Names the board World
board.name = "World";
//math to make X variable grow to board_size_x
for (int x = 0; x < board_size_x_; x++)
{
//math to make Y variable grow to board_size_y
for (int z = 0; z < board_size_z_; z++)
{
Transform cube = (Instantiate(cube_prefab_, new Vector3(x, 0, z), Quaternion.identity) as Transform);
cube.name = "Cube" + x + "," + z;
cube.parent = board.transform;
//pushes tiles into boardCubes array
boardCubes[b++] = cube;
//iterates through each newCube and debugs its name
foreach (Transform newCube in boardCubes)
{
Debug.Log("Added Cube: " + cube.name);
}
//iterates through each boardCubes and debugs its value
//for (int i = 0; i < 10; i++)
// Debug.Log(boardCubes[x]);
}
}
}
}
Answer by DaveA · Mar 30, 2011 at 11:25 PM
You might look into using Array or List, but I think what you want to basically do is stuff them in the array as you go, something like:
cube.parent = board.transform;
boardCubes[b++] = cube;
where b starts as 0. Using the array type you have, you may need to pre-size it to the number of cubes you'll be making (board_size_x_ * board_size_z_)
Thats apparently what I needed to push them into the Array, thank you.
I added the necessary code, and then did this:
foreach (Transform newCube in boardCubes)
{
Debug.Log("Added Cube: " + cube.name);
}
but I get back 40~ or so Debug.Log's per cube. (ie, Added Cube: Cube0,0 40 times, then Cube0,1 another 40) is this just how Debug.Log outputs in this situation or am I actually running the code too much?