Array Index Out of Range Exception after build
I have a method which errors with the Array IndexOutOfRangeException after the game is built. This problem does not occur when it is run in the editor; or when it is built in developer mode.
Even more confusingly the problem is fixed when I add a Z+1/Z-1 pair of lines (shown in comments below)
private readonly T[,] topRight;
private readonly T[,] topLeft;
private readonly T[,] bottomRight;
private readonly T[,] bottomLeft;
public T GetIndex(int Z, int X) {
//Z = Z + 1;
//Z = Z - 1;
if (Z >= 0 && X >= 0) {
return topRight[Z, X];
} else if (Z >= 0 && X < 0) {
return topLeft[Z, -X];
} else if (Z < 0 && X >= 0) {
return bottomRight[-Z, X];
} else if (Z < 0 && X < 0) {
return bottomLeft[-Z, -X];
} else {
return default(T);
}
}
Adding comment outputs suggests it is this line which breaks. Strangely the other 3 are fine.
return topLeft[Z, -X];
Things I have already tried:
Checking that the requested value is smaller than the array size, and >= zero
Removing the again Z+1/Z-1 to see if making changes has fixed it
Outputting the values which are being tried in the array - they are always reasonable values
My guess is there is a simplification made by the unity build-compiler which is a mistake. But that's really unlikely. So I'm out of ideas.
My version of unity is 2017.4.2f2