Unity Serialized Dictionary `Index Out Of Range` after 12 items
<p><strong>Update:</strong> This is a confirmed bug from Unity and it has been filed.</p>

<p>I have created a generic serializable dictionary and a specific one for int:Object. </p>

<pre><code>using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;

/// &lt;summary&gt;
/// Is what it sounds like
/// &lt;/summary&gt;
[System.Serializable]
public class SerializableDictionary&lt;TKey,TValue&gt; : Dictionary&lt;TKey,TValue&gt;, ISerializationCallbackReceiver 
{
  [SerializeField]
  protected List&lt;TKey&gt; keys = new List&lt;TKey&gt;();

  [SerializeField]
  protected List&lt;TValue&gt; values = new List&lt;TValue&gt;();

  //Save the dictionary to lists
  public void OnBeforeSerialize()
  {
    keys.Clear();
    values.Clear();
    foreach(KeyValuePair&lt;TKey,TValue&gt; pair in this)
    {
        keys.Add(pair.Key);
        values.Add(pair.Value);
    }
  }

  //Load the dictionary from lists
  public void OnAfterDeserialize()
  {
    this.Clear();

    if(keys.Count != values.Count)
    {
      throw new System.Exception(string.Format("there are {0} keys and {1} values after deserialization. Make sure that both key and value types are serializable."));
    }

    for(int i = 0; i&lt;keys.Count; i++)
    {
      this.Add(keys[i], values[i]);
    }
  }
}

[System.Serializable]
public class DictionaryOfStringAndInt : SerializableDictionary&lt;string, int&gt; { }

[System.Serializable]
public class DictionaryOfIntAndSerializableObject : SerializableDictionary&lt;int, ScriptableObject&gt; 
{
}
</code></pre>

<p>However the compiler goes crazy with the error</p>

<blockquote>
  <p>IndexOutOfRangeException: Array index is out of range.
  System.Collections.Generic.Dictionary`2+Enumerator[System.Int32,UnityEngine.ScriptableObject].MoveNext
  ()</p>
</blockquote>

<p>For some reason this line</p>

<p><code>foreach(KeyValuePair&lt;TKey,TValue&gt; pair in this)</code></p>

<p>Throws that error but ONLY AFTER my <code>DictionaryOfIntAndSerializableObject</code> exceeds 12 objects. This baffles me completely.</p>

<p>Can you think of a reason why this <code>DictionaryOfIntAndSerializableObject</code> is throwing an out of range exception after 12 items?</p>

<p><strong>Update</strong> This post has the same issue: <a href="http://forum.unity3d.com/threads/serializeable-dictionary-exceptions-after-code-change.319285/" rel="noreferrer">http://forum.unity3d.com/threads/serializeable-dictionary-exceptions-after-code-change.319285/</a></p>

<p><strong>Further Explanation</strong></p>

<p>I have a database GameObject that holds all my model (type) data. And what I am doing is I parse CSV files into <code>ScriptableObjects</code> that I store inside the database GameObject. That way my database is compiled once at runtime and I can freely access it at both runtime and in the editor. This database object is what has the <code>DictionaryOfIntAndSerializableObject</code> objects. I have one for every table (so I have one for weapons, armor, etc).</p>

<p>Here is the base class of all my scriptable objects (that get stored as the value in the dictionary):</p>

<pre><code>public class M_Card : ScriptableObject

{

  public E_CardTypes cardTypeID;

  public int rankID;

  public int rarityID;

  public int qualityID;

  public int editionID;

  public Sprite sprite;

 public M_Card()
 {
    cardTypeID                  = 0;
    rankID                      = 0;
    qualityID                   = 0;
    sprite = null;
 }

}
</code></pre>

<p>And here is the code where (after parsing the a row from the CSV) I put an instance of M_Card into the dictionary:</p>

<pre><code> private void LoadActionCards()
 {
    List&lt;Dictionary&lt;string, object&gt;&gt; listData = CSVReader.Read("Actions");

    for (var i = 0; i &lt; listData.Count; i++)
    {
      Dictionary&lt;string, object&gt; data = listData[i];

      string name = (string)data["Name"];
      M_ActionCard card = ScriptableObjectCreator.createActionCard(name);

      int cardTypeID  = (int)data["CardTypeID"];
      card.cardTypeID = (E_CardTypes)cardTypeID;

      AssignActionCardValues(card, data);

      Database.ACTION_CARD_LIST.Add(card);

      // THIS IS WHERE I MAP THE TYPEID TO THE M_CARD OBJECT
      // THIS IS WHERE THE OBJECT IS ADDED TO THE DICTIONARY
      Database.ACTION_CARD_MAP[card.ID] = card;
    }
  }
</code></pre>

<p>So right now I have 4 tables of data (weapons, helmets, armor, classes). However, if any of those tables has more than 12 items I get that error 100% on the 13th item. I am still investigating but heres how keys are added to the dictionary.</p>

<p>Note that the M_HelmetCard is just the same as an M_ActionCard and they are both just empty subclasses of the M_Card.</p>

<p>Here are some pictures to add a little more proof. In the first I have all 16 helmets. The error appears
<a href="https://i.stack.imgur.com/jMgZn.png" rel="noreferrer"><img src="https://i.stack.imgur.com/jMgZn.png" alt="enter image description here"></a></p>

<p>Then here I make it so there is only 12 and the error is gone. 
<a href="https://i.stack.imgur.com/gnvxO.png" rel="noreferrer"><img src="https://i.stack.imgur.com/gnvxO.png" alt="enter image description here"></a></p>

<p>Any list that reaches more than 12 errors out. The actual items don't matter (so its not like a specific 13th item is corrupted, 13 of the same items still cause this error).</p>

<p>Very baffled at the moment.</p>

<p>(I thought maybe the issue was I had a <code>Sprite</code> class that was getting serialized and was too much data, but when I got rid of it the problem persisted).</p>

<p><strong>Update</strong> Here is a link to a sample project which has this issue (everything irrelevant removed). <a href="https://www.mediafire.com/?axxn3u823cm7c59" rel="noreferrer">https://www.mediafire.com/?axxn3u823cm7c59</a></p>

<p>To produce the error:
Click the <code>database</code> object in the heirachy and click <code>Update Database</code> button.</p>

<p>Then just hit play and you will see the error.</p>

<p>Then if you want to see how its the 13th item, open the Helmets CSV file (<code>\Assets\Resources\ExcelData</code>), and remove the 13th item. Then update the database again. Then hit play. The error will be gone!</p>

<p>This is a major WTF :?</p>