# how can i check if index exists?

I am trying to check if there is an object to any side of it in the 3d array. My function to check is :

``````void UpdateSides ()
{
Vector3 myPos = transform.position;
int x = (int) myPos.x;
int y = (int) myPos.y;
int z = (int) myPos.z;

if (levelData[x, y, z] != null)
{
if ( x < levelData.GetLength(0) && y < levelData.GetLength(1) && z < levelData.GetLength(2))
{
print ("Pass GetLength Test");
if (x-1>-1 && y-1>-1 && z-1>-1)
{
print ("Pass Over 0");

if (levelData[x, y+1, z] == null)
{
print ("Nothing Above");
}
if (levelData[x, y+1, z] != null)
{
print ("Something Above");
}
if (levelData[x, y-1, z] == null)
{
print ("Nothing Below");
}
if (levelData[x, y-1, z] != null)
{
print ("Something Below");
}
if (levelData[x+1, y, z] == null)
{
print ("Nothing Right");
}
if (levelData[x+1, y, z] != null)
{
print ("Something Right");
}
if (levelData[x-1, y, z] == null)
{
print ("Nothing Left");
}
if (levelData[x-1, y, z] != null)
{
print ("Something Left");
}
if (levelData[x, y, z+1] == null)
{
print ("Nothing Forward");
}
if (levelData[x, y, z+1] != null)
{
print ("Something Forward");
}
if (levelData[x, y, z-1] == null)
{
print ("Nothing Back");
}
if (levelData[x, y, z-1] != null)
{
print ("Something Back");
}
}
}
}
}
``````

I have been trying to figure out what i am doing wrong and i just cant seem to find it. Im trying everything. Im using one of the lines that was recommended and now it still isnt working.

Try this:

``````if ( x < levelData.GetLength(0)
&& y < levelData.GetLength(1)
&& z < levelData.GetLength(2))
{
//your code
}
``````

Thank you ahmedbenlakhdhar. I used your code. The only thing i had to fix was that i put a -1 at the end to get one shorter of the length. The code i used is:

``````void UpdateSides ()
{
Vector3 myPos = transform.position;
int x = (int) myPos.x;
int y = (int) myPos.y;
int z = (int) myPos.z;

if (levelData[x, y, z] != null)
{
if (x < levelData.GetLength(0)-1)
{
if (x-1>-1)
{
if (levelData[x+1, y, z] == null)
{
print ("Nothing Right");
}
if (levelData[x+1, y, z] != null)
{
print ("Something Right");
}
if (levelData[x-1, y, z] == null)
{
print ("Nothing Left");
}
if (levelData[x-1, y, z] != null)
{
print ("Something Left");
}
}
}
if (y < levelData.GetLength(1)-1)
{
if (y-1>-1)
{
if (levelData[x, y+1, z] == null)
{
print ("Nothing Above");
}
if (levelData[x, y+1, z] != null)
{
print ("Something Above");
}
if (levelData[x, y-1, z] == null)
{
print ("Nothing Below");
}
if (levelData[x, y-1, z] != null)
{
print ("Something Below");
}
}
}
if (z < levelData.GetLength(2)-1)
{
if (z-1>-1)
{
if (levelData[x, y, z+1] == null)
{
print ("Nothing Forward");
}
if (levelData[x, y, z+1] != null)
{
print ("Something Forward");
}
if (levelData[x, y, z-1] == null)
{
print ("Nothing Back");
}
if (levelData[x, y, z-1] != null)
{
print ("Something Back");
}
}
}
}
}
``````