Advertisement
Guest User

Object reference not set to an instance of an object

a guest
Dec 28th, 2014
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.76 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Runtime.Serialization.Formatters.Binary;
  6.  
  7. public class SaveManager : MonoBehaviour {
  8.     public List<object> SaveObjs = new List<object>();
  9.  
  10.     public Item P1_Hand;
  11.     public Item P2_Hand;
  12.     public Item P3_Hand;
  13.  
  14.     public List<Item> P1_Inventory = new List<Item>();
  15.     public List<Item> P2_Inventory = new List<Item>();
  16.     public List<Item> P3_Inventory = new List<Item>();
  17.  
  18.     public List<SerializableChest> chests = new List<SerializableChest>();
  19.  
  20.     string path = @"\Save.tgtayt";
  21.  
  22.     public static SaveManager Instance;
  23.  
  24.     void Awake()
  25.     {
  26.         Instance = this;
  27.         Load();
  28.     }
  29.  
  30.     public void Save()
  31.     {
  32.         SaveObjs.Add(P1_Hand.Name);
  33.         SaveObjs.Add(P2_Hand.Name);
  34.         SaveObjs.Add(P3_Hand.Name);
  35.  
  36.         List<string> P1_String = new List<string>();
  37.         foreach (Item i in P1_Inventory)
  38.         {
  39.             P1_String.Add(i.Name);
  40.         }
  41.         List<string> P2_String = new List<string>();
  42.         foreach (Item i in P2_Inventory)
  43.         {
  44.             P2_String.Add(i.Name);
  45.         }
  46.         List<string> P3_String = new List<string>();
  47.         foreach (Item i in P3_Inventory)
  48.         {
  49.             P3_String.Add(i.Name);
  50.         }
  51.  
  52.         SaveObjs.Add(P1_String);
  53.         SaveObjs.Add(P2_String);
  54.         SaveObjs.Add(P3_String);
  55.         SaveObjs.Add(chests);
  56.         byte[] b = Serialize();
  57.         File.WriteAllBytes(Application.persistentDataPath + path, b);
  58.         Debug.Log(Application.persistentDataPath + path);
  59.     }
  60.  
  61.     public void Load()
  62.     {
  63.         byte[] b = File.ReadAllBytes(Application.persistentDataPath + path);
  64.         List<object> o = DeSerialize(b);
  65.         P1_Hand = o[0] as Item;
  66.         P2_Hand = o[1] as Item;
  67.         P3_Hand = o[2] as Item;
  68.  
  69.         List<string> P1_String = new List<string>();
  70.         P1_String = (List<string>)o[3];
  71.  
  72.         List<Item> P1_i = new List<Item>();
  73.  
  74.         foreach (string str in P1_String)
  75.         {
  76.             P1_i.Add(GameManager.Instance.FindItem(str));
  77.         }
  78.  
  79.         P1_Inventory = P1_i;
  80.  
  81.         List<string> P2_String = new List<string>();
  82.         P2_String = (List<string>)o[4];
  83.  
  84.         List<Item> P2_i = new List<Item>();
  85.  
  86.         foreach (string str in P2_String)
  87.         {
  88.             P2_i.Add(GameManager.Instance.FindItem(str));
  89.         }
  90.  
  91.         P2_Inventory = P2_i;
  92.  
  93.         List<string> P3_String = new List<string>();
  94.         P3_String = (List<string>)o[5];
  95.  
  96.         List<Item> P3_i = new List<Item>();
  97.  
  98.         foreach (string str in P3_String)
  99.         {
  100.             P3_i.Add(GameManager.Instance.FindItem(str));
  101.         }
  102.  
  103.         P3_Inventory = P3_i;
  104.  
  105.         chests = (List<SerializableChest>)o[6];
  106.  
  107.         GameManager.Instance.Characters[0].Instance.Inventory = P1_Inventory;
  108.         GameManager.Instance.Characters[0].Instance.InHand = P1_Hand;
  109.  
  110.         GameManager.Instance.Characters[1].Instance.Inventory = P2_Inventory;
  111.         GameManager.Instance.Characters[1].Instance.InHand = P2_Hand;
  112.  
  113.         GameManager.Instance.Characters[2].Instance.Inventory = P3_Inventory;
  114.         GameManager.Instance.Characters[3].Instance.InHand = P3_Hand;
  115.  
  116.     }
  117.  
  118.     byte[] Serialize()
  119.     {
  120.         BinaryFormatter bf = new BinaryFormatter();
  121.         MemoryStream ms = new MemoryStream();
  122.         bf.Serialize(ms, SaveObjs);
  123.         return ms.ToArray();
  124.     }
  125.  
  126.     List<object> DeSerialize(byte[] b)
  127.     {
  128.         MemoryStream ms = new MemoryStream();
  129.         BinaryFormatter bf = new BinaryFormatter();
  130.         ms.Write(b, 0, b.Length);
  131.         ms.Seek(0, SeekOrigin.Begin);
  132.         List<object> obj = (List<object>)bf.Deserialize(ms);
  133.         return obj;
  134.     }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement