How to reduce lag for cubes

Hey all, I am trying to generate a group of cubes without physics so that I can then program something to remove said cubes after they are generated on level launch. However, it produces immense lag. Does anyone know how I can reduce this?

or ( float startX = -64F; startX < 65F; startX ++ )
  {
  GameObject neuBrick = GameObject.CreatePrimitive(PrimitiveType.Cube);
  CurPos = new Vector3(startX,startY,startZ);
  // neuBrick.AddComponent<BoxCollider>();
  neuBrick.transform.Translate(CurPos);
  neuBrick.layer = 8;
  if ( startX == 64F )
  {
  startZ += 1F;
  startX = -64F;
  }
  }

That is not the most recent version, and I have it looping correctly but it creates too much lag. Does someone know how I can fix this?

Rather than calling CreatePrimitive() every time, try creating one cube, then cloning it. I’m not entirely sure on the overhead of CreatePrimitive (I really wouldn’t expect it to be very high) but it won’t be lower than cloning.

There’s no difference really; it amounts to the same thing. If you’re making thousands of cubes, then instantiating them all as individual objects is the wrong approach. Instead create meshes using the Mesh class, where each mesh contains many cubes.

–Eric

1 Like