- Home /
Question by
Jan_Julius · Oct 13, 2014 at 11:46 AM ·
insantiate
How can I shorten this repetitive code?
How can I shorten this code?
for(int i = 0; i < 3; i++){
Instantiate(Prefab, new Vector3(-3, 10f * i, 0), Quaternion.identity);
Instantiate(Prefab, new Vector3(3, 10f * i, 0), Quaternion.identity);
Instantiate(Prefab, new Vector3(1, 10f * i, 0), Quaternion.identity);
Instantiate(Prefab, new Vector3(-1, 10f * i, 0), Quaternion.identity);
Instantiate(Prefab, new Vector3(2, 10f * i, 0), Quaternion.identity);
Instantiate(Prefab, new Vector3(-2, 10f * i, 0), Quaternion.identity);
Instantiate(Prefab, new Vector3(0, 10f * i, 0), Quaternion.identity);
}
Thanks!
Comment
Best Answer
Answer by Bunny83 · Oct 13, 2014 at 11:50 AM
With a nested for loop:
for(int y = 0; y < 3; y++)
{
for(int x = -3; x <= 3; x++)
{
Instantiate(Prefab, new Vector3(x, 10f * y, 0), Quaternion.identity);
}
}
Maybe you want to multiply x also with a distance value, otherwise the objects have a distance of 1 in the x-axis
Your answer