So i’m trying to make a player saving and loading script, it was going good untill i got to where i need to save my generic list’s i have one for inventory, and equipment. But i am not having any luck saving this until i came along to a random post on a forum that I can’t remember where he showed and example of saving the contents of a generic list, but the list is in the same script, mine however is in another script, so does this code:
public void SaveInventory()
{
//Get a binary formatter
var b = new BinaryFormatter();
//Create an in memory stream
var m = new MemoryStream();
//Save the scores
b.Serialize(m, inv.mainInventory.Count);
//Add it to player prefs
PlayerPrefs.SetString("Inventory", Convert.ToBase64String(m.GetBuffer()));
}
public void LoadInventory()
{
//Get the data
var data = PlayerPrefs.GetString("Inventory");
//If not blank then load it
if(!string.IsNullOrEmpty(data))
{
//Binary formatter for loading back
var b = new BinaryFormatter();
//Create a memory stream with the data
var m = new MemoryStream(Convert.FromBase64String(data));
//Load back the scores
inv.mainInventory= (List<ItemHandler>)b.Deserialize(m) as List<ItemHandler>;
inv.mainInventory = inventoryStorage;
}
}
that is the snippet of the code i’m using everything else works about my list except for this so should I just put this in my inventory script as well as my equipment script and will that work? still not sure.
EDIT: So I tried putting it in the script where the List is, and nope same error, saves without any errors but when it loads it gives me this:
InvalidCastException: Cannot cast from source type to destination type.
Inventory.LoadInventory () (at Assets/Scripts/PlayerScripts/Inventory/Inventory.cs:514)
PauseMenu.OnGUI () (at Assets/PauseMenu.cs:37)
CS:514:
mainInventory = (List<ItemHandler>)b.Deserialize(m);
CS:37:
inv.LoadInventory();
I am 100% at a loss from this i tried casting as List still the same error so any help will be great, also please don’t tell me to google(or if you do tell me what I was SUPPOSED to find when doing that. God knows i love halting my game progress to just post on here instead of a supposed quick google…)