2
\$\begingroup\$

I don't get errors when playing from Unity "Game" tab. But when I build an executable (development or not) it stucks. I get "IndexOutOfRange" exception. Which also magically disappears (yes) when I add some debug lines:

This does not work:

for (int i = x1; i <= x2; i++)
for (int j = y1; j <= y2; j++)
{
    int a, b;
    a = 7 + i; // Out of range
    b = 7 + j; // Out of range

    submap[a, b] = world.WalkableMap[(int)CurrentWalkableTile.x + i, (int)CurrentWalkableTile.y + j];
}

This works:

for (int i = x1; i <= x2; i++)
for (int j = y1; j <= y2; j++)
{
    int a, b;
    a = 7 + i;
    b = 7 + j;

    if (a < 0 || a > 15) // now with this never gets out of range
        Debug.Log("A out of range: " + a);

    if (b < 0 || b > 15) // now with this never gets out of range
        Debug.Log("B out of range: " + b);

    submap[a, b] = world.WalkableMap[(int)CurrentWalkableTile.x + i, (int)CurrentWalkableTile.y + j];
}

I can't find any sense. The exception literally hides from me

Additional info maybe is useful:

x1 = (CurrentWalkableTile.x - 7) >= 0 ? -7 : -(int)CurrentWalkableTile.x;
x2 = (CurrentWalkableTile.x + 7) < world.wmap_x_size ? 7 : (world.wmap_x_size - (int)CurrentWalkableTile.x - 1);

y1 = (CurrentWalkableTile.y - 7) >= 0 ? -7 : -(int)CurrentWalkableTile.y;
y2 = (CurrentWalkableTile.y + 7) < world.wmap_y_size ? 7 : (world.wmap_y_size - (int)CurrentWalkableTile.y - 1);

I'd like to post more information, but I can't even debug to find the problem.

\$\endgroup\$
8
  • \$\begingroup\$ Out of bound errors are unpredictable, it may fail, it may not. Since you use indices, what is the actual bound of a and b? \$\endgroup\$
    – Majte
    Oct 28, 2015 at 2:04
  • \$\begingroup\$ a and b are supossed to be in [0,14]; both values are 7 +/- [0, 7], depending on [x1,x2] and [y1,y2] \$\endgroup\$
    – freesoul
    Oct 28, 2015 at 2:08
  • \$\begingroup\$ What container class? \$\endgroup\$
    – Majte
    Oct 28, 2015 at 2:13
  • \$\begingroup\$ submap is bool[15,15] \$\endgroup\$
    – freesoul
    Oct 28, 2015 at 2:16
  • 2
    \$\begingroup\$ You've encountered the elusive Heisenbug, a bug which disappears when you add instrumentation to catch it. Do you have any information about when the exception is being thrown? ie. is it immediate when the script loads/runs for the first time, or does it happen randomly sometime later during play? If the former, that may be a clue that there could be a race condition in the initialization of the array, where under some circumstances this code gets hit before the code which initializes it. Hard to say without seeing more of the code though. \$\endgroup\$
    – DMGregory
    Oct 28, 2015 at 2:41

1 Answer 1

1
\$\begingroup\$
if (a < 0 || a > 15) // now with this never gets out of range
            Debug.Log("A out of range: " + a);

if (b < 0 || b > 15) // now with this never gets out of range
            Debug.Log("B out of range: " + b);

submap is bool[15,15] (according to comments)

then that should be

if (b < 0 || b >= 15)

or

if (b < 0 || b > 14)

index 15 would be already outside of bounds (size 15, indizes 0 - 14)

Since you couldn't trace your bug with the current if statement this leads to the conclusion your invalid index causing the exception is 15 and not a negative index.


Since part one didn't find the problem the next chance is to not check the submap[a][b] array but the world.WalkableMap[] array.

Just do the same as with a b.

int c = (int)CurrentWalkableTile.x + i;
int d = (int)CurrentWalkableTile.y + j;

if (c < 0 || c >= world.WalkableMap.length) Debug.Log("C out of range: " + c); 

if (d < 0 || d >= world.WalkableMap[0].length) Debug.Log("d out of range: " + d);

Because you are using two arrays but so far you haven't excluded one causing the IndexOutOfBoundsException.

\$\endgroup\$
5
  • \$\begingroup\$ I fixed that and still the same \$\endgroup\$
    – freesoul
    Oct 28, 2015 at 11:25
  • \$\begingroup\$ Unity supports code that is only executed within the editor? Do you have such code? If index is out of bounds may the submap gets filled in editor only code and is zero zero or null in the build? \$\endgroup\$
    – monty
    Oct 28, 2015 at 12:10
  • \$\begingroup\$ What are the values of world.wmap_x_size world.wmap_y_size? \$\endgroup\$
    – monty
    Oct 28, 2015 at 12:16
  • \$\begingroup\$ I have no code which is supossed to run only in editor. Submap is never empty, i init it manually as full false. world.wmap_x_size = 96; world_wmap_y_size = 44. I tested it and it is ok \$\endgroup\$
    – freesoul
    Oct 28, 2015 at 14:29
  • \$\begingroup\$ Tricky tricky. Helping you further would need more questions than suitable for comments. May we find a oppurtunity to use the chat. Next approaches would be log everything (i,j,x1,x2,....) everything and check if submap[a,b] is out of bounds or the calculations on the right side. Put the results into vars and log them too \$\endgroup\$
    – monty
    Oct 28, 2015 at 17:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .