Hi, I have managed to save my inventory list somehow but so far it loads properly only every third time. I’m only saving and loading one file but the values are different every time i load them.
here is the Load function:
public void Load()
{
if (File.Exists (Application.persistentDataPath + "/playerData.dat")) {
BinaryFormatter bf = new BinaryFormatter ();
FileStream file = File.Open (Application.persistentDataPath + "/playerData.dat", FileMode.Open);
SaveData data = (SaveData)bf.Deserialize (file);
file.Close ();
DisableComponents ();
tempPosition.x = data.positionx;
tempPosition.y = data.positiony;
tempPosition.z = data.positionz;
player.transform.position = tempPosition;
if (inventory.inventory.Count > 0) {
foreach (Item item in inventory.inventory) {
Destroy (item.gameObject);
inventory.inventory.Remove (item);
}
}
if (data.itemIDs.Count > 0) {
for (int i = 0; i < data.itemIDs.Count; i++) {
foreach (Item it in itemList.itemList) {
if (it.ID == data.itemIDs *)*
_ inventory.AddItem (it, data.itemQty );_
* }*
* }*
* }*
* }*
* }*
and here is where i call it:
if (GUILayout.Button (“Load Game”)) {
* showGUI = false;*
* GameManager.gameManager.Load ();*
* GameManager.gameManager.SystemMessage (“Game loaded successfully.”);*
* }*
and the message “Game loaded successfully.” only appears when it loads properly, so every third time also. Can anyone please help me fix this problem?
Thanks in advance.