- Home /

**closed**Oct 18, 2011 at 06:09 PM by DavidDebnar for the following reason:

The question is answered, right answer was accepted

# 3D array problem

Hey guys... I have a problem I suddenly ran into... Arrays can't go lover than 0! Well it was stupid from me but still... I have an 3D array and the code asks it to go like planet[-1,0,0] and it crashes.. The code is below... Well how the hell can I make it else? All I wanted was just to make a planet/sphere made of cubes, with similar rendering to Minepackage, like if next cube is air/0 than make that face.. But than I ran into this array problem...Should I just leave the planets, or is there any other way? I generate the planet from the center and than just loop until it is a sphere. Than I make if cube's distance from the center is lower than radius*2 set it to air block - because of atmosphere - if it's in radius set it to dirt/1 for the actual planet. Any ideas?

David

import System.Collections.Generic;

var rebuild : boolean = false;

//var radius : int = Mathf.Round(Random.Range(30,500)); var radius : int = 50; var size = radius*2; var planet = new int[100,100,100];

function Start() { GeneratePlanet(); }

function GeneratePlanet() { var x : int = -size; var y : int = -size; var z : int = -size;

```
while(x <= size - 1)
{
while(y <= size - 1)
{
while(z <= size - 1)
{
var dist = Vector3.Distance((transform.position + Vector3(x,y,z)), transform.position);
print(Vector3(x,y,z));
if(dist <= radius * 2)
{
planet[x,y,z] = 0;
if(dist <= radius)
{
planet[x,y,z] = 1;
}
}
z++;
}
z= -size;
y++;
}
z=-size;
y=-size;
x++;
}
rebuild = true;
BuildPlanet();
```

}

function BuildPlanet() { var planetMesh : Mesh = GetComponent(MeshFilter).mesh; var planetVertices = new List.();

```
for(var x : int = 0; x <= size; x++)
{
for(var y : int = 0; y <= size; y++)
{
for(var z : int = 0; z <= size; z++)
{
if(planet[x,y,z] != 0 && planet[x,y,z] != null)
{
AddBlockToPlanet(x,y,z, planetVertices);
}
}
}
}
planetMesh.Clear();
planetMesh.vertices = planetVertices.ToArray();
planetMesh.RecalculateBounds();
rebuild = false;
```

}

function AddBlockToPlanet(x : int, y : int, z : int, planetVertices : List.) { if(planet[x+1,y,z] == 0) { AddBlockSideToPlanet( x+ .5, y -.5, z+.5, x+ .5, y -.5, z -.5, x+ .5, y+.5, z+.5, x+ .5, y+.5, z -.5, planetVertices); } if(planet[x-1,y,z] == 0) { AddBlockSideToPlanet( x- .5, y -.5, z+.5, x- .5, y -.5, z -.5, x- .5, y+.5, z+.5, x- .5, y+.5, z -.5, planetVertices); } if (planet[x,y+1,z] == 0) { AddBlockSideToPlanet( x+ .5, y+.5, z+.5, x+ .5, y+.5, z -.5, x- .5, y+ .5, z+.5, x- .5, y+ .5, z -.5, planetVertices); } if (planet[x,y-1,z] == 0) { AddBlockSideToPlanet( x+ .5,y -.5, z+.5, x+ .5,y -.5, z -.5, x- .5, y-.5, z+.5, x- .5, y-.5, z -.5, planetVertices); } if (planet[x,y,z+1] == 0) { AddBlockSideToPlanet( x+ .5,y-.5,z+.5, x- .5,y-.5,z +.5, x+ .5, y+.5, z+.5, x- .5, y+.5, z +.5, planetVertices); } }

function AddBlockSideToPlanet(x1 : int, y1 : int, z1 : int, x2 : int, y2 : int, z2 : int, x3 : int, y3 : int, z3 : int, x4 : int, y4 : int, z4 : int, planetVertices : List.) { planetVertices.Add(new Vector3(x1+transform.position.x, y1+transform.position.y, z1+transform.position.y)); planetVertices.Add(new Vector3(x2+transform.position.x, y2+transform.position.y, z2+transform.position.y)); planetVertices.Add(new Vector3(x3+transform.position.x, y3+transform.position.y, z3+transform.position.y)); planetVertices.Add(new Vector3(x4+transform.position.x, y4+transform.position.y, z4+transform.position.y)); }

Have you tried shifting your starting point to halfway through your array?

Well, but than I can't get the center from which I'm getting the distance

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.