- Home /
Try/Catch block stops exception from happening
I have a small project that has an 2d array of sprites and a script that adds stairs at certain places. It all works fine in Game View but when I run the .exe when I stand in certain places the stairs can't be seen. When I checked the output_log.txt I found several exceptions had been raised in the stair placing function:
Completed reload, in 0.061 seconds desktop: 1920x1080 60Hz; virtual: 1920x1080 at 0,0 Initializing input.
Input initialized.
Initialized touch support.
Platform assembly: E:\project\SoA\New Unity Project\SoA_Data\Managed\System.Core.dll (this message is harmless) Platform assembly: E:\project\SoA\New Unity Project\SoA_Data\Managed\System.dll (this message is harmless) IndexOutOfRangeException: Array index is out of range. at BoardManager.SetStair (Int32 x, Int32 y) [0x00000] in :0 at StairMaster.GetStairs () [0x00000] in :0 at BoardManager.UpdateVisbleTiles () [0x00000] in :0 at BoardManager.UpdateBoard () [0x00000] in :0 at BoardManager.Update () [0x00000] in :0
(Filename: Line: -1)
The file contains the same exception as many times as I encounter it. Seems pretty clear cut whats going on, I'm obviously not checking my ranges correctly, still seems weird that it doesn't happen when running the project in the editor Game View.
This is the offending code:
if (x >= 0 && x < COLUMNS && y >= 0 && y < ROWS)
m_VisibleTiles[x, y].GetComponent<SpriteRenderer>().sprite = m_LowerStair[0].GetComponent<SpriteRenderer>().sprite;
if ((x + 1) >= 0 && (x + 1) < COLUMNS && y >= 0 && y < ROWS)
m_VisibleTiles[x + 1, y].GetComponent<SpriteRenderer>().sprite = m_LowerStair[1].GetComponent<SpriteRenderer>().sprite;
if (x >= 0 && x < COLUMNS && (y - 1) >= 0 && (y - 1) < ROWS)
m_VisibleTiles[x, y - 1].GetComponent<SpriteRenderer>().sprite = m_LowerStair[2].GetComponent<SpriteRenderer>().sprite;
if ((x + 1) >= 0 && (x + 1) < COLUMNS && (y - 1) >= 0 && (y - 1) < ROWS)
m_VisibleTiles[x + 1, y - 1].GetComponent<SpriteRenderer>().sprite = m_LowerStair[3].GetComponent<SpriteRenderer>().sprite;
The array m_LowerStair is already asserted to have the required length so it has to be m_VisibleTiles that is being accessed outside its bounds.
Well I try to catch the exception like this:
try
{
if (x >= 0 && x < COLUMNS && y >= 0 && y < ROWS)
m_VisibleTiles[x, y].GetComponent<SpriteRenderer>().sprite = m_LowerStair[0].GetComponent<SpriteRenderer>().sprite;
if ((x + 1) >= 0 && (x + 1) < COLUMNS && y >= 0 && y < ROWS)
m_VisibleTiles[x + 1, y].GetComponent<SpriteRenderer>().sprite = m_LowerStair[1].GetComponent<SpriteRenderer>().sprite;
if (x >= 0 && x < COLUMNS && (y - 1) >= 0 && (y - 1) < ROWS)
m_VisibleTiles[x, y - 1].GetComponent<SpriteRenderer>().sprite = m_LowerStair[2].GetComponent<SpriteRenderer>().sprite;
if ((x + 1) >= 0 && (x + 1) < COLUMNS && (y - 1) >= 0 && (y - 1) < ROWS)
m_VisibleTiles[x + 1, y - 1].GetComponent<SpriteRenderer>().sprite = m_LowerStair[3].GetComponent<SpriteRenderer>().sprite;
}
catch ( IndexOutOfRangeException e )
{
print("x: " + x + " y: " + y);
print(e);
}
Nothing prints and the output_log.txt now looks like this:
Completed reload, in 0.058 seconds desktop: 1920x1080 60Hz; virtual: 1920x1080 at 0,0 Initializing input. Input initialized. Initialized touch support. Platform assembly: E:\project\SoA\New Unity Project\SoA_Data\Managed\System.Core.dll (this message is harmless) Platform assembly: E:\project\SoA\New Unity Project\SoA_Data\Managed\System.dll (this message is harmless)
My first thought was that prints just don't show up in the output_log.txt but the real kicker is this: the bug doesn't occur anymore. Can anyone help explain what's going on?
Are you actually using Assert functions to check the length of m_LowerStair? Assert functions only work in the editor & develop builds!
http://docs.unity3d.com/ScriptReference/Assertions.Assert.html
"All method calls will be conditionally included only in the development builds, unless explicitly specified (see BuildOptions.ForceEnableAssertions). The inclusion of the assertions is controlled by UNITY_ASSERTIONS define. "