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.
There are things in the Asset Store that will do this, here’s one: Align Editor 2 | Utilities Tools | Unity Asset Store There’s a free one called Array Modifier, probably more like that too.
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.