# Iterate through an array

How can I iterate through a Vector3 array, and for every Vector3, check to see whether there is a neighbouring one?

So for example if my array is (0,0,0), how can I check if (1,0,0) is there?

By neighbouring, I mean if they were 1,1,1 blocks, they would be touching. So an increase in 1 co-ordinate, but not the others.

Edit: Here is my new technique, using a 3D array. However, I get an error - ‘not a widening conversion.’ Could anyone shed any light on this?

``````   var cubeSide : float;
var cubePrefab : GameObject;
var cube : int[,,];

function Start()
{
cube = new int[cubeSide,cubeSide,cubeSide];
Generate();
}

function Generate()
{
for(var x = 0; x < cubeSide - 1; x++)
for(var y = 0; y < cubeSide - 1; y++)
for(var z = 0; z < cubeSide - 1; z++)
{
var t = Random.value;
t = Mathf.Round(t);
cube[x,y,z] = t;
}
Build();
Check();
}

function Check()
{

for(var x = 0; x < cubeSide - 1; x++)
for(var y = 0; y < cubeSide - 1; y++)
for(var z = 0; z < cubeSide - 1; z++)
{
if(z != cube.Length - 1) if(cube[x,y,z + 1] == 1) print(""); //there is something at the front
if(z != 0) if(cube[x,y,z - 1] == 1) print(""); //there is something at the back
if(x != 0) if(cube[x - 1,y,z] == 1) print(""); //there is something at the left
if(x != cube.Length - 1) if(cube[x + 1,y,z] == 1) print(""); //there is something at the right
if(y != cube.Length - 1) if(cube[x,y + 1,z] == 1) print(""); //there is something above
if(y != 0) if(cube[x,y - 1,z] == 1) print(""); //there is something underneath
}
Build();
}
``````

Vector coordinates are floating point quantities, so "neighbouring" doesn't make a whole lot of sense, but if (0,0,0) and (1,0,0) are "neighbouring", perhaps you can use the distance between them to determine this relationship?

I.e., for every vector in your array, check if there exists another vector in your array, the distance to which is 1. Something like this:

``````public void VectorTest()
{
Vector3[] vectors = new Vector3[100];

foreach (Vector3 targetVec in vectors)
{
foreach (Vector3 testedAgainst in vectors)
{
if(Vector3.Distance(targetVec, testedAgainst) == 1.0f)
// targetVec and testedAgainst are "neighbours"
}
}
}

``````

Please beware that this is grossly inefficient - it runs in N^2-time, because it traverses the full array in a nested manner. So it's liable to get slow if the amount of vectors is high. Also note that the distance between two vectors is the euclidian distance, i.e. what Vector3.Distance actually calculates is Sqrt((x2-x1)^2 + (y2-y1)^2 + (z2-z1)^2). But since you're testing to see if the distance is 1, it means that the squared distance is also 1. So you don't have to take the squareroot of that to determine the result. You could write your own distance formula for the purpose of skipping that particular calculation. It would speed the calculation up.

Starting from Christian’s approach, here is a more general and slightly optimized version of finding neighbouring vectors (C#):

``````public void VectorTest()
{
Vector3[] vectors = new Vector3[100];

for(int i=0;i<vectors.Length-1;i++)
{
// no need to compare twice agains smaller indices, so start at i+1
for(int j=i+1;j<vectors.Length;j++)
{
if(Vector3.Distance(vectors*, vectors[j]) <= 1.0f)*
``````

// vectors and vectors[j] are “neighbours”
}
}
}
Again, as Christian mentions, this assumes “neighbouring” is defined by the 3D distance of two vectors. If you mean something else by “neighbouring”, please elaborate.

The best way would be to use a for loop and compare each next element’s x value against the current elements x + 1.

``````    function compareEachElement() : boolean {
var trueCount : int;
for(var i = 0; i < myArray.length; i++){
//check the that next elements x value is equal to the current elements x + 1
var currentElement = myArray*;*
``````

var nextElement = myArray[i + 1];
if( nextElement .x == currentElement.x + 1)
trueCount ++;
}
if(trueCount == myArray.length) return true;
}
if you wanted to compare a specific element in the array, then pass in an index to compare like so:
function compareSpecificElement( index : int ) : boolean {
var specificElement = myArray[ index ];
var nextElement = myArray[index + 1];
if( nextElement .x == specificElement.x + 1)
return true;
}

(Responding to the current code example, which no longer has a lot to do with the bulk of the question)

When using this:

``````t = Mathf.Round(t);
``````

, “t” is still a float. Assigning that to an int cube element will give the error you posted.

``````      var t = Random.value;