- Home /
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 levelData.GetLength( index ) where the index is the dimension of your array See: http://stackoverflow.com/questions/2044591/what-is-the-difference-between-array-getlength-and-array-length
Answer by Theacesofspades · Dec 09, 2014 at 11:01 PM
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");
}
}
}
}
}
Answer by ahmedbenlakhdhar · Dec 08, 2014 at 10:31 PM
Try this:
if ( x < levelData.GetLength(0)
&& y < levelData.GetLength(1)
&& z < levelData.GetLength(2))
{
//your code
}
im still getting an error saying the index is out of range at this line:
if (levelData[x, y-1, z] == null)
Did you use GetLength
in all of the conditions ?
I put in exactly what you have. Ill edit my question