Skip to main content
Clearer title. Reformatted code to remove extraneous indentation.
Source Link
Anko
  • 13.5k
  • 10
  • 56
  • 82

In Unity, why does my IndexOutOfRange exception which dissapearsdisappear when you trytrying to debug (Unity)it?

       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];
        }
        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 figure out where isfind the problem.

IndexOutOfRange exception which dissapears when you try to debug (Unity)

       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];
        }
        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 figure out where is the problem

In Unity, why does my IndexOutOfRange exception disappear when trying to debug it?

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];
}
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.

Source Link
freesoul
  • 393
  • 5
  • 12

IndexOutOfRange exception which dissapears when you try to debug (Unity)

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 figure out where is the problem