title: Array of dictionaries with Unity and Firebase database - Get values from key
body: <p>First of all - I am pretty new to Unity.</p>
  
  <p>I am trying to retrieve some data from my firebase database, store the data in an array/list of dictionaries, and then use the array/list to show the data from my server.</p>
  
  <p><strong>So... My way of trying to do:</strong></p>
  
  <p>1: Create array of dictionaries to hold my data:</p>
  
  <pre><code>[System.Serializable]
  public class Global
  {
     public static Dictionary&lt;string, object&gt;[] offers;
  }
  </code></pre>
  
  <p>2: Handle the data from the database, and store it in the array:</p>
  
  <pre><code>void Handle_ChildAdded(object sender, ChildChangedEventArgs e)
      {
          if (e.DatabaseError != null)
          {
              Debug.LogError(e.DatabaseError.Message);
              return;
          }
          // Do something with the data in args.Snapshot
          if (e.Snapshot.Value != null)
          {
              var dict = e.Snapshot.Value as Dictionary&lt;string, object&gt;;
  
              if (dict != null)
              {
                  Debug.Log(dict);
                  Global.offers = new Dictionary&lt;string, object&gt;[Global.storesCount+1];
                  Global.offers[Global.storesCount] = dict;
                  Global.storesCount++;
                  hasHaded = true;
              }
          }
      }
  </code></pre>
  
  <p>So now I have alle the snapshots from my database in my Global.offers array. The struckture of the snapshot i should get look something likes this: 
  <a href="https://i.stack.imgur.com/l4Et8.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/l4Et8.png" alt="enter image description here"></a></p>
  
  <p><strong>Time to show the data from my array</strong></p>
  
  <p>Everything works fine until this point - Because now I need to show the data I just stored in my Global.offers array.
  I try to do that with a loop. I loop though the array and search for the keys from my database and Instantiate the data inside a prefab of a gameobject <strong>like this:</strong></p>
  
  <pre><code> for (int i = 0; i &lt; Global.storesCount; i++)
          {
              Transform scrollViewObj = Instantiate(prefab, new Vector3(0, (downSize * i) - firstY, 0), Quaternion.identity);
              scrollViewObj.transform.SetParent(scrollContent.transform, false);
              scrollViewObj.transform.Find("Overskift").gameObject.GetComponent&lt;Text&gt;().text = Global.offers[i]["Store"] as string;
              scrollViewObj.transform.Find("Text (1)").gameObject.GetComponent&lt;Text&gt;().text = Global.offers[i]["Headline"] as string;
              scrollViewObj.transform.Find("Text (2)").gameObject.GetComponent&lt;Text&gt;().text = "Din pris: " + Global.offers[i]["Price"] as string + " kr.";
              scrollViewObj.transform.Find("Text (3)").gameObject.GetComponent&lt;Text&gt;().text = "Spar: " + Global.offers[i]["AndresPris"] as string + " kr.";
          }
  </code></pre>
  
  <p>This is where I get the trouble. For some reason Global.offers[i]["Store"] as string == null, witch of course means that I can't instantiate the object. I get this error: </p>
  
  <p><em>NullReferenceException: Object reference not set to an instance of an object
  LoadOffers.Start () (at Assets/Scripts/LoadOffers.cs:36)</em></p>
  
  <p>It is so weird, because when I try to debug I get some conflicting results:</p>
  
  <p>The length of the array is 19. So it is not empty.</p>
  
  <p>When I try to Debug.Log the array out I get: </p>
  
  <p>System.Collections.Generic.Dictionary`2[System.String,System.Object][]</p>
  
  <p>But when I search for values with keys like: </p>
  
  <pre><code>Debug.Log(Global.offers[0]["Store"]);
  </code></pre>
  
  <p>All I get is null. Am I searching wrong after the values? Or can anyone else see what I am doing wrong?</p>