title: (Unity3D) Saving/Loading Player Inventory
body: <p>Whenever saving or loading via the Player script, the inventory always comes up blank on both sides, and I can't figure out where the problem is. The inventory uses the same system that is used in the Intermediate Inventory Tutorial by Unity, which works fine, but the inventory is always blank when loading.</p>
  
  <p><strong>Player</strong></p>
  
  <pre><code>public void Save(){
      Debug.Log ("Saving data (player / inventory)");
  
      SaveLoadManager.SavePlayer (this); //works fine
      SaveLoadInventory.SaveInventory (inv); //where the issue is
  }
  public void Load(){
      Debug.Log ("Loading data (player / inventory)");
  
      float[] loadedStats = SaveLoadManager.LoadPlayer (); //works fine
      Item[] loadedItems = SaveLoadInventory.LoadInventory (); //comes up empty
  
      inv.items = loadedItems; //try to set the referenced Inventory script array of items equal to the loaded items
  
      //loadedStats[...] are set to player values and all works good
  }
  </code></pre>
  
  <p><strong>Inventory</strong></p>
  
  <pre><code>public class Inventory : MonoBehaviour {
  
  public Image[] itemImages = new Image[numItemSlots];
  public Text[] itemAmountsText = new Text[numItemSlots];
  public int[] itemAmounts = new int[numItemSlots];
  public Item[] items = new Item[numItemSlots];
  
  public const int numItemSlots = 40;
  
  public void AddItem(Item itemToAdd){
      for (int i = 0; i &lt; items.Length; i++) {
          if(items [i] == itemToAdd){
              if (itemAmounts [i] &lt;= 99) {
                  itemAmounts [i]++;
              } else {
                  items [i] = itemToAdd;
                  itemImages [i].sprite = itemToAdd.itemImage;
                  itemImages [i].enabled = true;
              }
              itemAmountsText [i].text = itemAmounts [i] + "";
              return;
          } else if (items [i] == null) {
              itemAmounts [i]++;
              itemAmountsText [i].text = itemAmounts [i] + "";
              items [i] = itemToAdd;
              itemImages [i].sprite = itemToAdd.itemImage;
              itemImages [i].enabled = true;
              return;
          }
      }
  }
  
  public void RemoveItem(Item itemToRemove){
      for (int i = 0; i &lt; items.Length; i++) {
          if (items [i] == itemToRemove) {
              items [i] = null;
              itemImages [i].sprite = null;
              itemImages [i].enabled = false;
              return;
          }
      }
  }
  
  public void ClearAllItems(){
      for (int i = 0; i &lt; items.Length; i++) {
          items [i] = null;
          itemImages [i].sprite = null;
          itemImages [i].enabled = false;
          return;
      }
  }
  
  [Serializable]
  public class InventoryData {
      public Image[] itemImages = new Image[numItemSlots];
      public Text[] itemAmountsText = new Text[numItemSlots];
      public int[] itemAmounts = new int[numItemSlots];
      public Item[] items;
  
      public InventoryData(Inventory inv){
          items = new Item[numItemSlots];
  
          for (int i = 0; i &lt; items.Length; i++) {
              inv.items[i] = items[i];
              inv.itemAmounts[i] = itemAmounts[i];
              inv.itemAmountsText[i] = itemAmountsText[i];
              inv.itemImages[i] = itemImages[i];
              }
          }
      }
  }
  </code></pre>
  
  <p><strong>SaveLoadInventory</strong></p>
  
  <pre><code>public static class SaveLoadInventory {
  
      public static void SaveInventory(Inventory inv){
  
      BinaryFormatter bf = new BinaryFormatter();
      var createdir = Directory.CreateDirectory(Application.persistentDataPath + "/saves");
      String dir = Application.persistentDataPath + "/saves";
      Inventory.InventoryData data = new Inventory.InventoryData (inv);
  
      DirectoryInfo dir_i = new DirectoryInfo(dir);
      FileInfo[] info = dir_i.GetFiles("*.inv");
      foreach (FileInfo f in info) {
          Debug.Log (f);
      }
  
      FileStream stream = new FileStream (dir + "/player" + (info.Length + 1) + ".inv", FileMode.Create);
  
      bf.Serialize (stream, data);
      stream.Close ();
  
  }
  
      public static Item[] LoadInventory(){
      String dir = Application.persistentDataPath + "/saves";
  
      DirectoryInfo dir_i = new DirectoryInfo(dir);
      FileInfo[] info = dir_i.GetFiles("*.inv");
      foreach (FileInfo f in info) {
          Debug.Log (f);
      }
  
      if (info.Length &gt;= 1) {
  
          BinaryFormatter bf = new BinaryFormatter ();
          var createdir = Directory.CreateDirectory(Application.persistentDataPath + "/saves");
          FileStream stream = new FileStream (dir + "/player" + (info.Length) + ".inv", FileMode.Open);
          Inventory.InventoryData data = bf.Deserialize (stream) as Inventory.InventoryData;
  
          stream.Close ();
          return data.items;
      } else {
          return null;
      }
  }
  </code></pre>
  
  <p>I honestly would try some combination of Debugging for this and figure it out myself but it all seems too complicated to wrap my head around and have been worried it wouldn't ever work. If you have any help or suggestions that would be greatly appreciated.</p>