[SOLVED] Save and Load Script not working properly

I have made a Saving and Loading script called “SaveLoad.” I have also made a simple Pause menu and in the pause menu there is a Save button. When you click the Save button, the game is supposed to save but it isn’t. I am calling the SaveGame() method. And yes, the method SaveGame() is public. I will post the SaveGame script. This has worked for me before but it isn’t working for me now. I do not know why. Loading is working fine but saving isn’t. I want the game to save my game data to a class and then load it from that class. Just look at the script. I get no errors and everything is referenced correctly:

using UnityEngine;
using System.Collections;
using System.IO;
using System;
using System.Runtime.Serialization.Formatters.Binary;

public class SaveLoad : MonoBehaviour 
{
    //Class References
    public Ammo ammo;
    public EconomySystem ecoSystem;
    public static SaveLoad saveLoad;

    void Awake ()
    {
        if (saveLoad == null)
        {
            saveLoad = this;
        } 
        else if (saveLoad != this)
        {
            Destroy(gameObject);
        }
    }

    void Start ()
    {
        LoadGame();
    }

    public void SaveGame ()
    {
        //Main Save Information
        BinaryFormatter binaryFormat = new BinaryFormatter();
        string gameSaveDirectory = "C:/Vlentful Studios/ZomB Apoc/";
        string gameSaveName = "ZomB Apoc - Game Data.apoc";

        //Checks to make sure game directory exists
        if (!Directory.Exists(gameSaveDirectory))
        {
            Directory.CreateDirectory(gameSaveDirectory);
        }

        //More Save Information
        FileStream gameSave = File.Create(gameSaveDirectory + gameSaveName);
        GameData gameData = new GameData();

        //This is what gets saved
        gameData.amountOfAmmo = ammo.amountOfAmmo;
        gameData.amountOfAmmoClips = ammo.amountOfAmmoClips;

        gameData.amountOfZomBCash = ecoSystem.amountOfZomBCash;

        binaryFormat.Serialize(gameSave, gameData);
        gameSave.Close();
    }

    public void LoadGame()
    {
        //Main Load Information
        string gameSaveDirectory = "C:/Vlentful Studios/ZomB Apoc/";
        string gameSaveName = "ZomB Apoc - Game Data.apoc";

        //Checks to make sure save file exists
        if (File.Exists(gameSaveDirectory + gameSaveName))
        {
            BinaryFormatter binaryFormat = new BinaryFormatter();
            FileStream gameSave = File.Open(gameSaveDirectory + gameSaveName, FileMode.Open);
            GameData gameData = (GameData)binaryFormat.Deserialize(gameSave);

            gameSave.Close();

            ammo.amountOfAmmo = gameData.amountOfAmmo;
            ammo.amountOfAmmoClips = gameData.amountOfAmmoClips;

            ecoSystem.amountOfZomBCash = gameData.amountOfZomBCash;
        }
    }
}

[Serializable]
class GameData
{
    //Ammo Information
    public int amountOfAmmo = 10;
    public int amountOfAmmoClips = 5;

    //EconomySystem Information
    public int amountOfZomBCash = 0;
}

EDIT:
Never mind. I am sorry about this. I fixed this problem right after I posted this. I have updated the script for any one who might need a saving script like this. I am sorry! And it was the loading part that caused the problem.

1 Like

Thanks man saves me the trouble of going through it myself. Any chance you could take a crack at my thread? You seem knowledgeable.

For what it’s worth, I’m currently using Serialize with a BinaryFormatter for my High Frontier game, and regretting it. The format is not human-readable, and it appears to be rather fragile; changes to your structures can completely break the ability to read old files, unless you jump through some fairly intricate hoops.

So, I’m thinking about replacing it with something based on JSON, which I’ve been quite happy with on other projects. (Though not in Unity yet — so suggestions as to a good JSON writer/parser will be appreciated!)

Love the JSON.Net package on the assetstore, there’s also litJson which is free ( http://wiki.unity3d.com/index.php?title=UnityLitJSON ). I haven’t run in any issue with either of them but my use cases are pretty basic.

Thanks Doom, those look like a couple of great options. I’ll probably switch to one of those in the next month or so (as soon as I get this darned physics wrangling squared away!).

Okay, sorry everyone.
@Troas
I will take a look at your thread.

@JoeStrout
I use this format because it is harder for people to hack your game. I understand what you mean though, it has been annoying before but I prefer this because it is harder to mod a game to get unlimited money or whatever you put there to save.

@DoomSamurai
Thanks for posting this. I will take a look at it as I will be honest, I have never tried .JSON files but I know what they are.