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.