# How do I divide integers properly?

I’m trying to make it look like the one in pic 1

but I’m getting a result which is in pic 2 SEG 2,

what it is doing is dividing all the cubes (28 of them 7x4 instead the bottom 8 and up)

and i dont know how to stop that,

lets say i extend the 7x4 to 8x4 it will give me the one in pic2 SEG 1 instead of pic 1 SEG 2, here is my code below of pic 2

1 - What i’m trying to achieve

[62966-untitled5.png*|62966]

2 - The result I’m getting

[62967-untitled2.png*|62967]

My code (public `int` set to: divideAmount is 2, mapSizeX is 7, mapSizeY is 4)

``````using UnityEngine;
using System.Collections;

public class CubeGenerator : MonoBehaviour
{

public Transform tilePrefab;
public int mapSizeY;
public int mapSizeX;
Transform newTile;
public int boxCount;
public int divideAmount;
public Material mat;
GameObject[] box;

[Range(0, 1)]
public float CubeSize;

void Start()
{
GenerateMap();

}
void Update()
{

}

public void GenerateMap()
{

string holderName = "Generated Cubes";
if (transform.FindChild(holderName))
{
DestroyImmediate(transform.FindChild(holderName).gameObject);
}

Transform mapHolder = new GameObject(holderName).transform;
mapHolder.parent = transform;

for (int x = 0; x < mapSizeX; x++)
{
for (int y = 0; y < mapSizeY; y++)
{
Vector3 tilePosition = new Vector3(x * 1, y * 1, 0);
newTile = Instantiate(tilePrefab, tilePosition, Quaternion.identity) as Transform;

newTile.localScale = Vector3.one * (1 - CubeSize);

newTile.parent = mapHolder;

}
}
Box = GameObject.FindGameObjectsWithTag("Box");
BoxCount = Box.Length;

for (int i = 0; i < boxCount; i++)
{

if ((i % divideAmount) == 0f)
{
box*.GetComponent<Renderer>().material = mat;*
``````

}
}

}
}
*
*

Firstly, to maintain control and to be sure that the boxes are in expected order in the array I would put them there manually instead of the heavyish `FindGameObjectsWithTag` call. (This probably isn’t messing up anything for you now, but still…)

``````BoxCount = mapSizeX * mapSizeY;
Boxes =new GameObject[BoxCount];
for (int x = 0; x < mapSizeX; x++)
{
for (int y = 0; y < mapSizeY; y++)
{
....
Boxes[(y*mapSizeX) + x] = newTile;
``````

Secondly, if you want vertical rows independent of grid dimensions, you have to do modulo only on the x- coordinate.

``````         for (int i = 0; i < BoxCount; i++)
{
int xcoord = i % mapSizeX;
if ((xcoord % DivideAmount) == 0f)
{
Box*.GetComponent<Renderer>().material = mat;*
``````

}
}

using UnityEngine;
using System.Collections;

``````public class CubeGenerator : MonoBehaviour
{
public Transform cubePrefab;
[Range(0, 1)]
public float cubeSize = 1;
public int division = 2;
public Material material;
public int mapSizeX;
public int mapSizeY;
// if cubeCount == 0 then count limit is ignored
public int cubeCount = 0;						// map will be not fully filled with cubes if [cubeCount < mapSizeX * mapSizeY]

// you can use handler array for future use
private Renderer[][] _cubeRenderers = null;		// keep all handlers to the generated box renderers and tranforms, can be null

void Start()
{
GenerateMap();
}

public void GenerateMap()
{
const string holderName = "Generated Cubes";

// destroy old cubes if exists
Transform mapHolder = transform.FindChild(holderName);
if (mapHolder != null)
{
// destroyImmediate should be used by the Editor only!!!
Destroy(mapHolder.gameObject);
}

// create new map holder
mapHolder = new GameObject(holderName).transform;
mapHolder.parent = transform;

// init hendler array (rows)
_cubeRenderers = new Renderer[mapSizeX][];

int count = 0;
for (int x = 0; x < mapSizeX; x++)
{
// init hendler array (cols)
_cubeRenderers[x] = new Renderer[mapSizeY];

for (int y = 0; y < mapSizeY; y++)
{
// check current cube count
if(cubeCount > 0)	// if enabled
{
if(count < cubeCount)
{
// increase the count
count ++;
}
else
{
break;
}
}

// create new cube
Transform cube = Instantiate(cubePrefab, new Vector3(x, y, 0), Quaternion.identity) as Transform;

cube.localScale = Vector3.one * cubeSize;
cube.parent = mapHolder;

// keep handler for this cube renderer
_cubeRenderers[x][y] = cube.GetComponent<Renderer>();

// set new material for cube if required
if (x % division == 0f)
{
_cubeRenderers[x][y].material = material;
}
}
}

// destroy prefab if not used anymore
Destroy(cubePrefab.gameObject);
}

public Renderer[][] cubeRenderers
{
get {
return _cubeRenderers;
}
}
}
``````