Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Bizio · Sep 06, 2017 at 11:39 AM · matrixindexout of range

Can I check if pair of indexes are in range of the matrix?

Hi there, I'm making strategy game with tiles - every tile has unique index number that it gets recognized with and i have a matrix of those indexes that represends whole board. It looks like so (it's smaller sample to make is easier to understand).

     int[,] fieldIndexMatrix = new int[3, 3];
 
     /*
         after initialization with indexes, example!
      \ y
     x \    0     1     2
        \___________________
         |     |     |     |
      0  |  0  |  1  |  2  |
         |_____|_____|_____|
         |     |     |     |
      1  |  3  |  4  |  5  |
         |_____|_____|_____|
         |     |     |     |
      2  |  6  |  7  |  8  |
         |_____|_____|_____|
     */
 


So in the middle is field with index 4 and you get it by fieldIndexMatrix[1, 1], bottom left is field with index 6 and you get it by fieldIndexMatrix[2, 0], you get the idea :)

And let's now say that i want some effect that will affect some targeted field and adjacent ones. Area of effect will be + looking so i need to find right, left, bottom, and upper field relative to targeted one.

I can find them by doing so:

         List<int> adjacentIndexes = new List<int>();
 
         int x;// x coordinate of the matrix
         int y;// y coordinate of the matrix

         int a;
         int b;

         a = x + 1;
         b = y;
 
         adjacentIndexes.Add(fieldIndexMatrix[a, b]);
 
         a = x - 1;
         b = y;
 
         adjacentIndexes.Add(fieldIndexMatrix[a, b]);
 
         a = x;
         b = y + 1;
 
         adjacentIndexes.Add(fieldIndexMatrix[a, b]);
 
         a = x;
         b = y - 1;
 
         adjacentIndexes.Add(fieldIndexMatrix[a, b]);
 
         return adjacentIndexes.ToArray();

While it will work for the fields in the middle It won't for the fields on the edge. If my targeted field has coordinate x = 0 and I'll try to find x - 1 I'll get and error "IndexOutOfRangeException: Array index is out of range." and my script will just stop there. but i don't want it to stop!!!

I want it to keep going and finding indexes. If it can't find an index because the coordinates for the matrix are out of range I just want to ignore this part and go to the next step. Is there some function or trick that can tell me If array index is in range of the matrix or not?

I tried doing

 if(fieldIndexMatrix[a, b] != null)
       adjacentIndexes.Add(fieldIndexMatrix[a, b]);

but it does not seem to work. Any Idea?

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by akillingbeck · Sep 06, 2017 at 11:58 AM

fieldIndexMatrix .GetLength() function, passing in the dimension you want to test will give you the size.

Using that with a < 0 check before you use fieldIndexMatrix[a, b] will avoid out of range exceptions

Comment
Add comment · Share
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
0

Answer by Bizio · Sep 06, 2017 at 12:09 PM

yeah getLength, thats what i was looking for thanks :)

Comment
Add comment · Share
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

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

70 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Array of transform index out of range error 1 Answer

How to check if a c# 3d array index exists? 1 Answer

How do I check a List[0], when it's nothing? 1 Answer

Array index out of range after build 1 Answer

Array index is out of range Error, only when array is used to Instantiate 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