Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by startselect_ · Jul 10, 2015 at 10:02 AM · exceptionindexoutofrangeexceptiontry-catch

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?

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image TimHeijden · Aug 05, 2015 at 10:20 PM 0
Share

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. "

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How to properly catch Unity Assertion failures? 1 Answer

.TXT File loading error 1 Answer

Why is the try catch method in Unity not preventing my app from quitting/crashing? 1 Answer

Catch Exception from a failed Unity Command 0 Answers

Stop Game on Exception 4 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges