- Home /
Making a wall of cubes
I'm trying to create a wall of cubes with this code, but for some reason its skipped the first spot and start it into second with the "0-0" like in the the image.
float cubeWidth = 0.98f;
float cubeHeight = 0.98f;
void Start()
{
GameObject cube = new GameObject();
-- reference for the next instances
float referenceX = -2.938817f;
float referenceY = -2.109755f;
float referenceZ = 3.623962e-05f;
for (int i = 0; i <= 5; i++)
{
for (int j = 0; j <= 6; j++)
{
Vector3 position_ = new Vector3(referenceX +
j * cubeWidth, referenceY + i * cubeHeight, 0);
cube.transform.position = position_;
cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
--show their names
cube.name = "" + i + "-" + j;
}
}
}
Answer by robertbu · Jan 24, 2013 at 04:35 AM
You are setting the position in the transform before you are creating the cube. The first time through the loop, no cube object exists. In all other passes through the loop, you are setting the position of the previous cube before you create the new cube. Swap these two lines, and everything should work.
cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
cube.transform.position = position_;
Answer by aldonaletto · Jan 24, 2013 at 04:44 AM
You're setting the cube position before creating it! Swap the lines where you define the cube position and create the cube primitive:
for (int i = 0; i <= 5; i++){
for (int j = 0; j <= 6; j++){
Vector3 position_ = new Vector3(referenceX +
j * cubeWidth, referenceY + i * cubeHeight, 0);
// create the cube first...
cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
// and then set its position:
cube.transform.position = position_;
--show their names
cube.name = "" + i + "-" + j;
}
Your answer
Follow this Question
Related Questions
How to make a wall inpassible 1 Answer
Dynamically tiling texture on a cube of changing scale. 1 Answer
How do I curve a cube? 1 Answer
How to destroy the cube which I selected with the arrow key 2 Answers
Cube glitches when it moves into wall 2 Answers