How to enabled a specific gameObject in array?
I have an array of gameobjects called chunks[ , , ]. All the gameobjects in the array are inactive. I need to set a specific gameobject enabled. How can I do this? Im using:
world.chunks[chunkX, chunkY, chunkZ].SetActive(true);
This will not work. I just get a null reference exception. Can anyone help me? Thank you
How are you adding them to the array because it looks like the index is not set to a gameobject. So I think your logic is sound with calling SetActive on an item in the array but that value is null so that assumes it has not been set or it has been destroyed.
I go through the array to fill it.
void CreateChunks ()
{
chunks=new GameObject[$$anonymous$$athf.FloorToInt(worldX/chunkSize),
$$anonymous$$athf.FloorToInt(worldY/chunkSize),
$$anonymous$$athf.FloorToInt(worldZ/chunkSize)];
for (int x=0; x<chunks.GetLength(0); x++)
{
for (int y=0; y<chunks.GetLength(1); y++)
{
for (int z=0; z<chunks.GetLength(2); z++)
{
chunks[x,y,z] = Instantiate(chunk,new Vector3(x*chunkSize,y*chunkSize,z*chunkSize),new Quaternion(0,0,0,0)) as GameObject;
chunks[x,y,z].transform.parent = transform;
chunks[x, y, z].SetActive(false);
ChunkScript newChunkScript= chunks[x,y,z].GetComponent<ChunkScript>();
newChunkScript.worldGO=gameObject;
newChunkScript.chunkSize=chunkSize;
newChunkScript.chunkX=x*chunkSize;
newChunkScript.chunkY=y*chunkSize;
newChunkScript.chunkZ=z*chunkSize;
}
}
}
}
That looks ok...is this a race condition? Are you sure that CreateChunks is called before you try to access it? Are these objects destroyed in anyway?
In not destroying them im just setting them to not active
just a lucky guess, but maybe world is null, the rest looks fine.