private IEnumerator CheckForChunkLoadCoroutine() { while (true) { var worldPosition = player.position; var chunkPosition = worldGrid.ChunkPosition(worldPosition); var chunksToUnload = new List <int2>(); foreach (var loadedChunkPosition in worldGrid.LoadedChunkPositions) { var isInRadius = loadedChunkPosition.x <= chunkPosition.x + chunkLoadRadius && loadedChunkPosition.x >= chunkPosition.x - chunkLoadRadius && loadedChunkPosition.y <= chunkPosition.y + chunkLoadRadius && loadedChunkPosition.y >= chunkPosition.y - chunkLoadRadius; if (!isInRadius) { chunksToUnload.Add(loadedChunkPosition); } } foreach (var chunkToUnload in chunksToUnload) { worldGrid.UnloadChunk(chunkToUnload); } for (var x = -chunkLoadRadius; x <= chunkLoadRadius; x++) { for (var y = -chunkLoadRadius; y <= chunkLoadRadius; y++) { var offsetedChunkPosition = chunkPosition + int2(x, y); if (!worldGrid.ChunkIsLoaded(offsetedChunkPosition)) { worldGrid.LoadChunk(offsetedChunkPosition); } } } yield return(new WaitForSeconds(0.1f)); } }