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 palalopea · Jan 12, 2018 at 06:37 AM · arraylistmatrix

IndexOutOfRangeException: Array index is out of range.

I have list and array. i think i created wrong script. I want to show data profile of building after I calculate the value of integer like matrix. but still error in array. can someone help me? thanks

 public double[] _Capacity, _Equipment, _Rooms, _Parking;
         private List<Mylist> MyList = new List<Mylist>();
     
         public GameObject listPrefab;
         public Transform listParent;


  for (int i = 0; i < MyList.Count; i++)
         {
 
              _saw[0, i] = (_Parking[i] * wParking) + (_Rooms[i] * wRooms) + (_Capacity[i] * wCapacity) +
                         (_Equipment[i] * wEquipment);
             _saw[1, i] = i;
                 
 
         }
         bool flag = true;
         double tmp;
         for (int i = 1; (i < MyList.Count - 1) && flag; i++)
         {
             flag = false;
             for (int j = 0; j < MyList.Count - 1; j++)
             {
                 if (_saw[0, j + 1] > _saw[0, j])
                 {
                     tmp = _saw[0, j];
                     _saw[0, j] = _saw[0, j + 1];
                     _saw[0, j + 1] = tmp;
                     tmp = _saw[1, j];
                     _saw[1, j] = _saw[1, j + 1];
                     _saw[1, j + 1] = tmp;
                     flag = true;
                          }
             }
         }
 
   
             for (int i = 0; i < 3; i++)
             {
                 int index = (int)_saw[1, i];
                
                 GameObject tmpObject = Instantiate(listPrefab);
 
                 Mylist tmpList = MyList[index];

                 tmpObject.GetComponent<ListScript>().SetList(tmpList.Name, tmpList.Location.ToString(), tmpList.Capacity.ToString(), tmpList.Rooms.ToString(), tmpList.Equipment.ToString(), tmpList.Parking.ToString(), tmpList.Status.ToString());
 
                 tmpObject.transform.SetParent(listParent);
                 Debug.Log(MyList[index]);
             }




Comment
Add comment · Show 4
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 Legend_Bacon · Jan 12, 2018 at 09:27 AM 0
Share

Hello there,

You have a lot of things going on there. COuld you provide the full error message (with line numbers) so it's easier to help you? Also, try putting some debug logs in there and tell us which ones get or don't get triggered.

Thank you!

Cheers,

LegendBacon

avatar image palalopea Legend_Bacon · Jan 12, 2018 at 04:25 PM 0
Share

the error in :

 for (int i = 0; i < 3; i++)
              {
                  int index = (int)_saw[1, i];

I already used debug logs but nothing happens :'( can you share me an example how to use list and array ? and I used sqlite to get data.

avatar image Legend_Bacon palalopea · Jan 12, 2018 at 05:25 PM 0
Share

In this for loop you use "3" as a hard-coded number, but are you sure that "_saw" always has that many at least? For example, an index out of range exception would happen if you tried to access the third element when there are only two.

As for Lists, This looks like a pretty good tutorial. You might also want to take a look at This, as you are using two-dimensional arrays here.

I hope that helps!

~Cheers,

LegendBacon

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by W01Fi3 · Jan 13, 2018 at 07:09 AM

As far as I can tell, the problem should be that there just isn't enough elements within the array.

  for (int i = 0; i < 3; i++)

When you try to access say an array with only two elements in it, so

 AnArray[3] = code;

It will return the error "Array index is out of range".

Basically, make sure when you're using arrays, make sure the index actually has an element.

Also, the first element starts with the index of 0, then element 2 has index of 1, and so on.

Hope this helps in any way

Comment
Add comment · Show 1 · 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 palalopea · Jan 13, 2018 at 07:26 AM 0
Share

how about I want to use 3 rows only from list to show? my list<> is :

 private List<$$anonymous$$ylist> $$anonymous$$yList = new List<$$anonymous$$ylist>();


I want to use sqlite. should I coding this script into :

 using (IDataReader reader = dbCmd.ExecuteReader())
           {
                      while (reader.Read())
                      {
                      **THE SCRIPTS**
                      }
           }

 

?

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

86 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 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

A node in a childnode? 1 Answer

Storing instances of a script in a list. 0 Answers

Looping through Button Array and checking for OnClick 0 Answers

Sorting a list by distance to an object? 1 Answer

Inspector variables from class in List are always initialized to its default values 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