Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 GutoThomas · May 02, 2012 at 10:09 PM · listrangeindexargumentout

Find the correct index number for array inside two loops

Hey everyone. I'm trying to make some simple inventory with GUI.Box or something. I've made an List of type 'Item' wich is a custom class that control every single slot and I need to access each index of this list when I open the inventory. For this, I'm using the following code:

 public int rows, columns;
 
 void Start () {
 
     for(int x = 0; x < rows; x++) {
 
         for(int y = 0; y < columns; y++) {
 
         int index = ???
         Inventory[index].inUse = false;
 
         }
 
     }
 
 }

index = x + (y * columns) used to work but just when is a square-like inventory, like 5x5. Anybody have an idea of an algorithm that can calculate the current index based on x and y and in order to access the List ? Otherway I just receive 'Argument is out of range' errors, wich makes total sense.

Any help would be appreaciated! Thanks from now!

Here's a image of what the index should looks like(don't need to start with 1).

125alt text125

Example.jpg (27.3 kB)
Comment
Add comment · Show 5
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 Eric5h5 · May 02, 2012 at 10:31 PM 0
Share

Why not just use a 2D array?

avatar image GutoThomas · May 02, 2012 at 10:33 PM 0
Share

Eric5h5, I've done a lot of progress in this system already and I think if I rebuild the base I'll have too much work to put everything to work again. With 2D array you mean an Array of Vector2?

avatar image Eric5h5 · May 02, 2012 at 10:41 PM 0
Share

No, just a 2D array.

 inventory = new Item[rows, columns];
avatar image GutoThomas · May 02, 2012 at 10:43 PM 0
Share

That can be a possibility, too! Works both with array and lists?

avatar image Eric5h5 · May 02, 2012 at 10:48 PM 0
Share

No, it's a 2D array. There isn't such a thing as a 2D List per se, though you could have a List of Lists and accomplish basically the same thing.

2 Replies

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

Answer by Bunny83 · May 02, 2012 at 10:57 PM

It's very confusing when you swap x and y. x is usually always horizontal (from left to right). Here's how i would go about it:

 public int rows = 3;
 public int columns = 4;
 
 void Start ()
 {
     for(int y = 0; y < rows; y++) {
         for(int x = 0; x < columns; x++) {
             int index = x + (y*columns);
             Inventory[index].inUse = false;
         }
     }
 }

This will give you an index from 0 to 11 (12 elements == rows*columns)

The important thing is that the next row starts at the next index where the last column ends. first row: 0 1 2 3 so the second row starts at 4 so all indices have to be offset by 4. 4 is actually the length of 1 row (the column count). So the offset is just the row index multiplied with the column count.

  x + y * columns
  0 + 0 * 4       ==  0
  1 + 0 * 4       ==  1
  2 + 0 * 4       ==  2
  3 + 0 * 4       ==  3
  0 + 1 * 4       ==  4
  1 + 1 * 4       ==  5
  2 + 1 * 4       ==  6
  3 + 1 * 4       ==  7
  0 + 2 * 4       ==  8
  1 + 2 * 4       ==  9
  2 + 2 * 4       == 10
  3 + 2 * 4       == 11
Comment
Add comment · Show 3 · 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 GutoThomas · May 02, 2012 at 11:04 PM 0
Share

That works perfectly! I don't know what I was doing wrong, maybe I got confused about the rows and columns... Thank you and thanks to everyone wich posted here to help me! Also, thanks for the explanation about a loop inside another.

avatar image Macronaut123 · Oct 20, 2012 at 09:27 PM 0
Share

$$anonymous$$an what an incredibly simple tutorial, you just wouldn't be able to believe HOW $$anonymous$$UCH this helped me to understand arrays, you're incredible really I don't have even words to describe how glad I am since I've been studying hard for months already.

Wish you all the best and I just created this account to thank you! haha XD

avatar image Bunny83 · Oct 20, 2012 at 10:28 PM 0
Share

@$$anonymous$$acronaut123:
:D Thanks, i'm glad i could help :)

avatar image
0

Answer by rutter · May 02, 2012 at 10:28 PM

For each row you've passed, you'll need to add the number of items per row to your index -- this happens to equal the number of columns in your grid. This seems to match your existing formula:

 index = x + (y * columns);

Take a look at your loops, though. You're using x to track rows (vertical movement), and y to track columns (horizontal movement). Is that what you wanted?

Remember also that arrays are indexed from zero. If I understand your plans correctly, that box you've labelled "1" is Inventory[0]. Seems like you understand that, but it's good to be sure. ;)

Comment
Add comment · Show 2 · 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 GutoThomas · May 02, 2012 at 10:40 PM 0
Share

rutter, thanks for the reply, I'll double check the formula's work. Yes, at least to me the x (row) and y (column) makes total sense to me. Row isn't a line( ___ ) wich represents x axis? If I wrote something wrong is probably because I'm brazilian and not an English pro yet. The part of the array index I'm doing correctly. I tried to add the index.ToString() to the box in order to display the current index and it doesn't comes straight forward, but I'll double check this.

avatar image Bunny83 · May 02, 2012 at 11:04 PM 0
Share

Yes a row is a line that goes from left to right, but the x coordinate specifies the position along this line therefore which column. y tells you which row you want to access(from top to bottom).

At least that's the usual geometric / mathematical definition ;)

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

7 People are following this question.

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

Related Questions

A node in a childnode? 1 Answer

Random object, Array index out of range 0 Answers

Wierd Animation Bug 0 Answers

array problem index out of range ? why ? 2 Answers

ArgumentOutOfRange Exception 1 Answer


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