- Home /
Instantiate a cube for each grid point into the scene
Hello i want to know how i can instantiate a cube for each grid point well i start the game
I tried to create a script but i dont have any idea how i can do it
Thanks if you help me
Answer by metalted · Apr 03, 2019 at 03:58 PM
Just a guess but if you dont know how to do that then did you make the grid by hand?
The simplest way to do it is using a nested for loop:
GameObject cube;
int width = 5;
int height = 5;
Gameobject[,] cubes = new GameObject[width, height];
for(int x = 0; x < width; x++){
for(int y = 0; y < height; y++){
cubes[x, y] = Instantiate(cube,
new Vector3(x, 0, y), Quaternion.identity);
}
}
If you make the spheres in the same way, you can then use a nested for loop to get all the positions of those sphere, and instantiate a cube there
That worked fine thanks and the grid i use this tutorial https://www.youtube.com/watch?v=VBZFYGWvm4A
Your answer
![](https://koobas.hobune.stream/wayback/20220612194758im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Generate a grid button 0 Answers
Best way to instantiate and render a grid of text 0 Answers
Grid-based building system not working 1 Answer
How do you store instantiated tiles using a coordinate system? 2 Answers
Auto-move on grid and draw line problem 0 Answers