SOLVED
I have been stuck on this problem for quite some time now and I really can not get it to work. I’ll share the code first and then explain the problem as I believe that the problem only makes sence once you have seen the code.
Vector3Int neighborChunk = m_chunkCoordinates;
if (face == 0)
{
neighborChunk.x = m_chunkCoordinates.x - 1;
}
else if (face == 1)
{
neighborChunk.z = m_chunkCoordinates.z - 1;
}
else if (face == 2)
{
neighborChunk.x += 1;
}
else if (face == 3)
{
neighborChunk.z += 1;
}
if (m_worldManager.isChunkInWorld(neighborChunk))
{
Debug.Log("Chunk does not exist: " + neighborChunk + " this chunk: " + m_chunkCoordinates + " face is " + face);
}
else {
Debug.Log("Chunk does exist: " + neighborChunk + " this chunk: "+ m_chunkCoordinates);
}
This is what is inside the m_worldManager.isChunkInWorld(neighborChunk) function:
public bool isChunkInWorld(Vector3Int coord) {
return m_chunkMap.ContainsKey(coord);
}
The dictionary declaration is looks like this: Dictionary<Vector3Int, Chunk> m_chunkMap
The wierd thing is that the the chunk is not found whenever the code enters the if 1 or 0 branch but the same chunk will be found if it takes route 2 or 3. This applies to every chunk. Thank you for your help.
Here is a screenshot of a chunk both existing and not existing at the same time. I am never removing things from the dictionary. (See first and last outputs)
EDIT: The problem was that the m_worldManager captured in each chunk was a copy and not a reference to the class that I passed in the constructor of each chunk. The world manager was assigned only when the chunks were initialized so the dictionary had not been completely filled and since the dictionary was only a copy and not a reference new chunk entries were not added to chunks that already had been initiated. Thus making the chunks that were initiated afterwards invisible to the previous chunks. Sorry for the bad explenation.
