- Home /
Creating a 68x68 grid of trigger cubes
I haven't posted here in awhile, but I need help with something. What i'm trying to do is create a grid of cubes that are invisible but represent places where i can place buildings. What i have done so far is create a prefab of a cube that is a trigger and doesn't have a mesh Renderer. But in the code i'm guessing that i would need to create a array of game objects and then create them from the prefab in a do while loop, But the problem is, I need to keep track of every cube so i can access them later. So i need to name each cube something different for instance the cube on the position would be called "cube 1x1" and then "cube 1x2" and so on for every cube. Anyone know how i would go about doing this? Thanks in advance.
Answer by iwaldrop · Jun 29, 2013 at 06:18 PM
Its pretty easy, as i hope you'll realize once you read this. Use a for loop. Two, actually, with the second nested within the first. The first one will handle x positioning while the second will handle z positioning (assuming its laid out on the x,z plane).
You need to know the dimensions of the squares, and how far apart they'll be spaced. With that, you have the delta between their origins. Add this delta to either the z or x position (respective of the innermost loop you're within). Create a game object with a name of string.Format("cube {0}x{1}", x, z), add your cube controller (which should require a box collider component), scale it properly and place it. The cube controller should notify the grid controller about itself in its awake method. The grid controller can then add the cube to a list. You can now control each cube independently (via their controllers) and the grid as a whole via the grid controller.
Some nifty things you can do include sending events to cubes that neighbor a newly added cube to do some fancy stuff, and easily de/serializing the cubes via the grid.
Answer by DaveA · Jun 29, 2013 at 06:24 PM
There are things in the Asset Store that will do this, here's one: http://u3d.as/content/equilibre-games/align-editor-2/1Am There's a free one called Array Modifier, probably more like that too.
Your answer
![](https://koobas.hobune.stream/wayback/20220613105137im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to check whether gameObject is on even ground? 1 Answer
Moving kinematic rigidbody trigger against kinematic rigidbody collider 0 Answers
Trouble with triggers and colliders 1 Answer
How do you trigger a specific collider using OnTriggerEnter? 2 Answers
multiple Objects Enter OnTriggerEnter 2 Answers