0

In my game, I have a Computer class with following variables:

 [System.Serializable]
 public class Computer : MonoBehaviour, ShopItem{
     public string model;
     public int price;
     public int speed;
     public int watt;
     public Sprite image;
 }

And I have a script that contains a list of Computer:

   List<Computer> computerList;

Then when I save the game, I pass all data to the DataClass and convert it to Json:

 public string SaveToJson(){
     //Create data class
     DataClass dataClass = new DataClass(...other parameters..., computerList);
     //Convert to json
     string jsonString = JsonUtility.ToJson(dataClass, false);
     //Save to PlayerPref
     PlayerPrefs.SetString("GameData", jsonString);
 }

I also stored it into a json file just to see the format. The computerList contains some instanceID like below.

 "computerList": [
         {
             "instanceID": 655388
         },
         {
             "instanceID": 654696
         },
         {
             "instanceID": 655502
         },
         {
             "instanceID": 654696
         }
     ],

When load game,I load the playerPref and convert it back to dataClass:

 public void LoadGame(){
     string result = PlayerPrefs.GetString("GameData");
     DataClass dataClass = JsonUtility.FromJson<DataClass>(result);  
     this.computerList = dataClass.computerList;
 }

But when I try to access the computerList, it gives me this error: ArgumentNullException: Argument cannot be null.

I also saved it in the localhost database, and when I load it back it gives the same error, so I guess it's nothing wrong about PlayerPrefs. Other variables like int, float, list does well, so the problem should be about custom classes. Any help would be appreciate.

The full error code is:

ArgumentNullException: Argument cannot be null. Parameter name: key System.Collections.Generic.Dictionary`2[ShopItem,System.Int32].ContainsKey (ShopItem key) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Collections.Generic/Dictionary.cs:280) InventoryManager.ProcessItemList () (at Assets/Script/CoreGame/Local Manager/InventoryManager.cs:54) InventoryManager.ShowInvnetory () (at Assets/Script/CoreGame/Local Manager/InventoryManager.cs:89) UnityEngine.Events.InvokableCall.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:166) UnityEngine.Events.UnityEvent.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent_0.cs:58) UnityEngine.UI.Button.Press () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:66) UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:108) UnityEngine.EventSystems.ExecuteEvents.Execute (IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:50) UnityEngine.EventSystems.ExecuteEvents.Execute[IPointerClickHandler] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.EventFunction`1 functor) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:261) UnityEngine.EventSystems.EventSystem:Update()

Ivan Mok
  • 11
  • 2
  • What is `DataClass`? – derHugo Apr 15 '19 at 05:09
  • Also `Computet` is a `MonoBehavior` and cannot be generated using `JSON`. The only way of creating instances of `MonoBehaviour` is by using `AddComponent`. And in `Computer` there seems to be no member called `instanceID`... – derHugo Apr 15 '19 at 05:16
  • Can you point out in which line exactly the exception is thrown? – derHugo Apr 15 '19 at 05:17
  • @derHugo it does really state where it leads to this error. When I double click it does not direct me to the code. – Ivan Mok Apr 15 '19 at 05:32
  • I am now doing it another way. I will give each computer instance an ID, then I only store the ID of the computer, instead of the computer instance. – Ivan Mok Apr 15 '19 at 05:34
  • It at least says the line numbers in `InventoryManager` ;) – derHugo Apr 15 '19 at 05:44

0 Answers0