The question is answered, right answer was accepted. I was able to figure it out on my own it was an issue solely with my code, though I'm not sure why as of yet.
Checking if an instance of a Script saved in 3d array is null
I apologize in advance if there is already and answer out there for this question but I couldn't come up with one.
I am procedurally generating a map and randomly instantiating game objects on certain locations and saving those objects scripts that I attached to all the objects into a 3D array that is a respective index of the entire map.
I am then trying to upon clicking on a point on the map check if there are any objects saved in the array for that location in 3D space.
The array is created in my game manager script like so.
int worldX = 16;
int worldY = 16;
int worldZ = 16;
public SceneObject[,,] SceneGo;
void Start(){
SceneGo = new SceneObject[worldX,worldY,worldZ];
}
Then in another script a random integer is called and based of that an object will be placed or more often no object will be created like so.
int rand = (int)Random.Range(0, world.SceneryObject.Length + world.SceneryFactor*10 );
if (rand < world.SceneryObject.Length) {
Vector3 pos = new Vector3(x+chunkX, y+chunkY+0.75f, z+chunkZ);
world.SceneGo[x + chunkX, y + chunkY, z + chunkZ] = (Instantiate(world.SceneryObject[rand],pos,Quaternion.identity) as GameObject).GetComponent<SceneObject>() ;
world.SceneGo[x + chunkX, y + chunkY, z + chunkZ].x = x+chunkX;
world.SceneGo[x + chunkX, y + chunkY, z + chunkZ].y = y+chunkY;
world.SceneGo[x + chunkX, y + chunkY, z + chunkZ].z = z+chunkZ;
world.SceneGo[x + chunkX, y + chunkY, z + chunkZ].index = world.sceneObjs;
world.sceneObjs++;
Then in another script on click a call will be made to see if an index of that array is null or not.
void CheckForObstruction(Vector3 position) {
int x = (int)position.x;
int y = (int)position.y;
int z = (int)position.z;
if(world.SceneGo[x,y,z]!=null)Debug.Log("Obstructing object found");
}
Currently I am using a much larger map than 16x16x16 so I don't know if the size of the array is the issue, but if there is a fix out there that doesn't require an entire rethinking of the way I am doing this I would much appreciate it.
Answer by DocLamb · Jul 03, 2016 at 04:44 PM
I doubt that this will be able to help anyone that is running into the same issue, but through messing around with my code and several steps of debugging i found that evidently the objects were being indexed to the (y-1) spot rather than the y spot, so in my check I simply made the adjustment and the script seems to be working now.