- Home /
Question by
JaamiParvez · May 25, 2017 at 11:28 PM ·
c#instantiateobjects
How to create objects that fill a specific area?
In figure below, the red box and gray-border are objects in scene.
How do I instantiate multiple instances of the red box and fill the inside of the gray border using c#, like the other figure below, without overlapping each other and within the gray-border?
Thanks
unityquestions.png
(18.1 kB)
unityquestions-1.png
(18.6 kB)
Comment
Answer by toddisarockstar · May 25, 2017 at 11:44 PM
you can use a loop inside of a loop to spawn things in a grid pattern here is a basic example:
int x;
int y;
float spread=2f;
x = 8;
while (x>0) {x--;
y = 4;
while (y>0) {y--;
Instantiate (prefab, new Vector3 (x * spread, y * spread, 0), Quaternion.identity);
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612122818im_/https://answers.unity.com/themes/thub/images/avi.jpg)