how do i instantiate multiple object object with line change with for loop.
hi boys and girls
how do i instantiate multiple object object with line change with for loop.
i am totally noob but in documents.
for (int i = 0; i < 10; i++)
Instantiate(prefab, new Vector3(i * 2.0f, 0, 0), Quaternion.identity);
there is this.
so if i want instantiate 30 prefabs do i just
for (int i = 0; i < 3; i++)
Instantiate(prefab, new Vector3(i * 1f, 0, 0), Quaternion.identity);
for (int i = 0; i < 3; i++)
Instantiate(prefab, new Vector3(i * 1f, 1, 3), Quaternion.identity);
for (int i = 0; i < 3; i++)
Instantiate(prefab, new Vector3(i * 1f, 2, 6), Quaternion.identity);
do i do it this way or is there simpler way. and if so how do i center the prefabs.
there was this it centers the prefabs but i dont know how do i do the line change
thanks.
Answer by NoseKills · Jun 19, 2016 at 09:36 AM
You already use loops to set the x coordinate. Do it for y as well. You don't really explain what you mean by centering, but if your objects are 1 unit wide like you code suggests, just don't start from 0,0 but instead from negative coordinates by a sufficient amount
int xCount = 3;
int yCount = 3;
float xCentering = -(xCount - 1) / 2f;
float yCentering = -(yCount - 1) / 2f;
for (int x = 0; x < xCount; x++) {
for (int y = 0; y < yCount; y++) {
Instantiate(prefab,
new Vector3(x + xCentering, y + yCentering, 0),
Quaternion.identity);
}
}
thanks for the answer. but how do i put let say 4 on top 3 on middle 2 to bottom. like this.
(by center i mean like this. but your script did well(the object were in middle of screen, but how do i put them like this .) so even if there is not even number it doesnt go like this.
Your answer
![](https://koobas.hobune.stream/wayback/20220612083211im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to teleport player to an Instantiated Prefab 1 Answer
Prefab Enemy2 to track all prefab Enemy1's Instantiates transforms: How? 1 Answer
instantiated prefab doesn't follow spawn rotation 1 Answer
How do I instantiate particles properly,How do I instantiate particles correctly? 0 Answers
Call function for instantated prefab. "Object reference not set" 1 Answer