I have an int variable called "Stash". When I beat the level, the game will save. When it saves, I want Stash goes up by however much my score was that level. Currently, my code isn't saving my stash value, and it just sets it to "0(stash default) + Score(whatever my score was that level)." How can I write within this code to make my goal happen?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
[RequireComponent(typeof(GameData))]
public class SaveScript : MonoBehaviour
{
private GameData gameData;
private string savePath;
void Start()
{
gameData = GetComponent<GameData>();
savePath = Application.persistentDataPath + "/gamesave.save";
}
public void SaveData()
{
var save = new Save()
{
SavedStash = gameData.Stash + gameData.Score //<------ (This code)
};
var binaryFormatter = new BinaryFormatter();
using (var fileStream = File.Create(savePath))
{
binaryFormatter.Serialize(fileStream, save);
}
Debug.Log("Data Saved");
}
public void LoadData()
{
if (File.Exists(savePath))
{
Save save;
var binaryFormatter = new BinaryFormatter();
using (var fileStream = File.Open(savePath, FileMode.Open))
{
save = (Save)binaryFormatter.Deserialize(fileStream);
}
gameData.Stash = save.SavedStash; //<---------- (and this code)
gameData.ShowData();
Debug.Log("Data Loaded");
}
else
{
Debug.LogWarning("Save file doesn't exist");
}
}
}
Also relevant stuff in my GameData file:
public int Stash { get; set; }
public int StashNew { get; set; }
public int Score { get; set; }
The StashNew was just an idea I had to get the whole thing working.