Hello.

I´m trying to create a visualsiation of an physic experiment by a friend. Therefore I need a 3d grid out of a prefab-object. The user can change input values for the amount of objects in the grid, as well as the size of the grid.

So, my problem is that from the given amount of objects I´m not able to create(instantiate) the right amount into the grid.

For example: my give amount is 100, but when I´m instantiating the grid, the amount of objects is 125, not 100…

Because I´m using a construction with a for-loop, I try to calculate the the amount for each dimension by taking the root of the given amount(2nd, and 3rd). The failure is in filling each row and line of the grid with objects, some should left empty, according to the amount, but I´m really not good with math (and english, too ;-)), so maybe someone could held me with that? Here is my code (JavaScript):

```
// The data array stores the angle-information for each object in the grid
// the datas are coming from the experiment, there is another function where
// I give this data array to the object array which is working fine.
//What I need is MatrixDataArray.length == MatrixObjektArray.length after calculating the grid
// Here itś not reproducing the right amount:
function GitterKalkulation ()
{
MatrixDataArray = new float[MatrixCount]; // the given amount coming from an GUI-Menue(MatrixCount)
if(Form == 0) //if dimension 1 is choosen in the GUI-Menue: only a line on the x-Achsis is made
{
Spalten = MatrixDataArray.length; // = amount of rows (x-Achsis)
Zeilen = 1; // = amount of lines (z-Achsis)
Ebenen = 1; // = amount of layers (y-Achsis)
}
if(Form == 1) //dimension 2: x,z
{
p = 1.0/2.0; //actually this is not used...
Spalten = Mathf.Round(Mathf.Sqrt(MatrixDataArray.length)); // here itś getting wrong
Zeilen = Spalten;
Ebenen = 1;
}
if(Form == 2) //dimension 3: x,y,z
{
p = 1.0/3.0;
Spalten = Mathf.Round(Mathf.Pow(MatrixDataArray.length, p)); // here too and itś not only becouse the Mathf.Round
Zeilen = Spalten;
Ebenen = Spalten;
}
//Just to know whats going on...
print("Spalten: "+Spalten+" / "+(Form+1)+".Wurzel: "+Mathf.Pow(MatrixDataArray.length, p)+" / Potenz: "+p);
stepSpalte = Breite / Spalten; //The user can change the size of the grid,
stepZeile = Tiefe / Zeilen; //therefore the space calculation between the objects
stepEbene = Hohe / Ebenen;
doBuild = true;
calcBuild = false;
}
function Gitterbauen ()
{
SpawnP = Instantiate(NullPrefab, Vector3(0, 0, 0),Quaternion.identity);
for(var y = 0; y < Ebenen; y++)
{
for(var z = 0; z < Zeilen; z++)
{
for(var x = 0; x < Spalten; x++)
{
newX = SpawnP.position.x + stepSpalte*x;
newY = SpawnP.position.y + stepEbene*y;
newZ = SpawnP.position.z + stepZeile*z;
NewMatrixObjekt = Instantiate(MatrixObjekt, Vector3(newX, newY, newZ),Quaternion.identity);
NewMatrixObjekt.transform.parent = SpawnP;
NewMatrixObjekt.name = "Objekt" + MatrixObjektArray.length;
MatrixObjektArray.Add(NewMatrixObjekt);
}
}
}
//Ignore this, I tried some calculation...
var wert = MatrixDataArray.length - Mathf.Pow(Mathf.Pow(MatrixDataArray.length, p), 2);
print("Wert: "+wert);
doBuild = false;
}
```

Thank you.