我在我的游戏中遇到了IndexOutOfRange异常,但只在Mac OS X上的独立版本中运行。它在Windows和OSX上的Unity编辑器中运行良好,并且可以在Windows和Android上独立运行。但是在OSX上,我得到以下异常:
IndexOutOfRangeException: Array index is out of range.
at WorldController.InstantiateChunk (Vector2i pos, .MeshBuildInfo[] meshes) [0x00072] in /Users/sargunster/Code/Unity Projects/block-game/Assets/Scripts/World/Objects/WorldController.cs:226
at WorldController.FixedUpdate () [0x00060] in /Users/sargunster/Code/Unity Projects/block-game/Assets/Scripts/World/Objects/WorldController.cs:79
以下是相关功能:
void InstantiateChunk(Vector2i pos, MeshBuildInfo[] meshes)
{
float worldX = pos.x * World.CHUNK_SIZE - .5f;
float worldZ = pos.z * World.CHUNK_SIZE - .5f;
// for each chunk in the column
for (int y = 0; y < World.WORLD_HEIGHT; y++)
{
float worldY = y * World.CHUNK_SIZE - .5f;
// instantiate the chunk
Vector3 position = new Vector3(worldX, worldY, worldZ);
string name = System.String.Format(" Chunk at ({0}, {1}, {2})", pos.x, y, pos.z);
// THIS IS WHERE IT HAPPENED
ChunkRenderer opaque = MakeChunk(position, "Opaque" + name, meshes[y].opaque, opaqueMaterial);
ChunkRenderer transparent = MakeChunk(position, "Transparent" + name, meshes[y].transparent, transparentMaterial);
Vector3i chunkPos = new Vector3i(pos.x, y, pos.z);
opaqueInstances.Add(chunkPos, opaque);
transparentInstances.Add(chunkPos, transparent);
}
}
我按照引用查找实例化meshes
的位置,其长度应为WORLD_HEIGHT
。
MeshBuildInfo[] meshes = new MeshBuildInfo[World.WORLD_HEIGHT];
因此,我进入调试器以查看异常发生时变量的确切值。调试器输出:
> meshes
Evaluation failed.
> 2+2
4
> meshes.Length
Evaluation failed.
> y
0
> position
{(-7168.5, -0.5, 4079.5)}
索引是0,我可以评估表达式就好......除了不起作用的数组。我无法得到它的长度或它的价值。
我不知道如何解决这个问题。 “评估失败”是什么意思?索引如何超出范围?为什么这只发生在OSX上?我该如何解决?
World.WORLD_HEIGHT
是一个值为16的常量。
答案 0 :(得分:0)
经过几天这个问题的沮丧,我终于解决了它(有点)。它仅在游戏构建为x86_64或Universal时才会发生。将架构设置更改为x86解决了它。必须是Unity的错误。