title: Why does List.Add() return an index error?
body: <p>For some reason, my C# script gives the following error message on the sortOrderList.Add() line:</p>
  <blockquote>
  <p>Index was out of range. Must be non-negative and less than the size of
  the collection.</p>
  </blockquote>
  <p>Here is the structure of the database I'm sorting.</p>
  <p><a href="https://i.stack.imgur.com/VsDM2.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/VsDM2.png" alt="enter image description here" /></a></p>
  <p>What the part of the script below is doing is making a list of index locations for each of the types that can be found at varying locations based on their load order.  List sortOrderList shows the order to load each of the the types in, where a for loop could iterate from 0 to sortOrderList.Count - 1 to account for all the different types in all the categories combined and access these later on in the script below.</p>
  <p>For instance, if the following are locations of different types which each have a varying method: List {type1 1, type2 2, type2 3, type1 4, type1 5, type2 6}, then where type2 loads first, sortOrderList is {2, 3, 6, 1, 4, 5}. type1 and type2 are to represent methods defined in the script below.</p>
  <pre><code>foreach (DataClasses.DependencyMethodEnum method in dataClasses.DependencyMethodOrder)//for order of dependencies; add SortOrderList after the localTypeNames have been completely filled
      {
          for (int i1 = 0; i1 &lt; categoryNames.Count; i1++)//for each category i1
          {
              for (int i2 = 0; i2 &lt; listsTypeNames[i1].Count; i2++)//for each type i2
              {
                  if (listsTypeMethods[i1][i2] == method)//if the dependency order aligns with the current type
                  {
                      int iterations = 0;
                      bool breaking = false;
  
                      for (int i4 = 0; i4 &lt; categoryNames.Count; i4++)//again for each category i4; this is to sort out where the original order would be on the main list Lists//IS INEFFECTIVE: CALLS THE SAME ONE INSIDE EACH CATEGORY
                      {
                          for (int i5 = 0; i5 &lt; listsTypeNames[i4].Count; i5++)//for each type i5
                          {
                              Debug.Log(localTypeNames.Count);//high number....
                              Debug.Log(sortOrderList.Count);//0... so why is there an error when I later use .Add?
  
                              if (listsTypeNames[i4][i5] == listsTypeNames[i1][i2] &amp;&amp; (sortOrderList.Count == 0 || (sortOrderList.Count != 0 &amp;&amp; listsTypeNames[i4][i5] != localTypeNames[sortOrderList[sortOrderList.Count - 1]])))
                              {
                                  Debug.Log(&quot;PASS&quot;);
                                  Debug.Log(&quot;ITERATIONS:&quot; + iterations);
                                  Debug.Log(&quot;SOL COUNT: &quot; + sortOrderList.Count);
  
                                  sortOrderList.Add(iterations);//adds where the original position would have been for the type in the order of where it is in the local list//ERROR HERE
  
                                  Debug.Log(&quot;sol &quot; + sortOrderList.Count + &quot; : &quot; + localTypeNames[sortOrderList[iterations]]);
                                  breaking = true;
                                  break;
                              }
  
                              iterations++;
                          }
  
                          if (breaking)
                          {
                              break;
                          }
                      }
                  }
              }
          }
      }
  </code></pre>
  <p>Why does this happen?  I'm not addressing a specific index on the sortOrderList.Add() layer, but it still gives the error.  Here's the log.</p>
  <p><a href="https://i.stack.imgur.com/Nej6g.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Nej6g.png" alt="enter image description here" /></a></p>
  <p>Thanks in advance!</p>