- Home /
How to store Transform in an Transform array ?
Hello. I am trying to code a little "world" generation system.
What I have is when you start new game world of a size you choose will be generated. The world is created from cubes (1x1x1).
What I can't figure out Is how do I put these created blocks into an array.
For example -> I have a world with a size of 10. So that means 10 blocks will be created on the X and Z coordinate. ( 100 blocks in total ) I want each block to be stored in and array.From this array the game will choose random number. So let's say I have 100 blocks and the game will choose 35 blocks where the trees will be created and 65 blocks will be empty.
Can anybody help me please ?
Here is my code for " world" generation.
public Transform groundBlock;
public Vector2 mapSize;
public Transform[] allGroundBlocks;
// Use this for initialization
void Start () {
GenerateMap();
}
void GenerateMap()
{
string holderName = "Generated Map";
if (transform.Find(holderName))
{
DestroyImmediate(transform.Find(holderName).gameObject);
}
Transform mapHolder = new GameObject(holderName).transform;
mapHolder.parent = transform;
for(int x = 0; x < mapSize.x; x++)
{
for(int y = 0; y < mapSize.y; y++)
{
Vector3 tilePosition = new Vector3(-mapSize.x / 2 + 0.5f + x, 0, -mapSize.y / 2 + 0.5f + y);
Transform newTile = Instantiate(groundBlock, tilePosition, Quaternion.Euler(Vector3.right)) as Transform;
newTile.parent = mapHolder;
}
}
}
}
Answer by Bleakmountain50 · Oct 24, 2018 at 11:51 PM
If you use a List instead of an Array you'll have a much easier time
all I did was swap out public Transform[] allGroundBlocks;
to public List<Transform> allGroundBlocks;
and added in a part so any instantiated object is added to the list
here's the completed code:
public Transform groundBlock;
public Vector2 mapSize;
public List<Transform> allGroundBlocks;
// Use this for initialization
void Start()
{
GenerateMap();
}
void GenerateMap()
{
string holderName = "Generated Map";
if (transform.Find(holderName))
{
DestroyImmediate(transform.Find(holderName).gameObject);
}
Transform mapHolder = new GameObject(holderName).transform;
mapHolder.parent = transform;
for (int x = 0; x < mapSize.x; x++)
{
for (int y = 0; y < mapSize.y; y++)
{
Vector3 tilePosition = new Vector3(-mapSize.x / 2 + 0.5f + x, 0, -mapSize.y / 2 + 0.5f + y);
Transform newTile = Instantiate(groundBlock, tilePosition, Quaternion.Euler(Vector3.right)) as Transform;
newTile.parent = mapHolder;
allGroundBlocks.Add(newTile);
}
}
}
}
Holy moly. It was so easy and I couldn't see it. It works ! :D Thank you.
Answer by ADustyOldMuffin · Oct 25, 2018 at 12:13 AM
If you wish to store them specifically in an array you will have to declare the size of the array. So in example at the beginning of your generate method put
allGroundBlocks = new Transform[(int)mapSize.x, (int)mapSize.y];
And you'll have a 2d array the size of the map, so when you wish to store your objects you will store them in the array with their x and y cords like so.
allGroundBlocks[x, y] = gameObject.transform;
You'll notice I have to use (int) because an array can only take integers in it's constructor, and you'll have to do the same with your transforms being stored in the array. In the future though I would not store the map size as a Vector2 I would store it has a length and width like a square, and then assuming it starts at 0 you can give them cords based on that.
EDIT: I added an example for adding to the array
Your answer
![](https://koobas.hobune.stream/wayback/20220612180905im_/https://answers.unity.com/themes/thub/images/avi.jpg)