- Home /
Build not working on some saves for binary formatter but works on others
I've got the game fairly completed, only that save and load functions have been a problem. I'm using the Binary Formatter version.
I have 3 scripts that save their own separate data, one for the coins, one for the shop, one for the score as they all take place on different scenes. the score and the coins scripts do not save and load properly on close but the shop data one does. I am at a loss as to why. The only primary difference between the scripts is that the shopUIManager save and load does not have a DoNotDestroy logic and the other 2 do.
Since the score and coins data are practically identical i'm only posting the coins one and then the shopUI one for comparison. The shop one works the coins one doesn't.
One piece of advice i was given for android builds was to make the "write permissions" in the build settings to external. I've tried it both ways and neither has worked.
using System.Collections; using System.Collections.Generic; using UnityEngine.UI; using UnityEngine; using System; using System.Runtime.Serialization.Formatters.Binary; using System.IO;
public class ShopUIManager : MonoBehaviour {
//this will be the area to save the current bools of the shop itself
public static ShopUIManager shopUIManager;
public int shopCoins = 0;
public Text shopCoinsText;
public GameObject TimeUpgrade50;
public bool TimeUpgrade50Vis = true;
public GameObject TimeUpgrade200;
public bool TimeUpgrade200Vis = false;
public GameObject TimeUpgrade500;
public bool TimeUpgrade500Vis = false;
public GameObject TimeUpgrade1000;
public bool TimeUpgrade1000Vis = false;
public GameObject TimeUpgrade3000;
public bool TimeUpgrade3000Vis = false;
public GameObject TimeUpgrade5000;
public bool TimeUpgrade5000Vis = false;
//public GameObject TimeUpgradeMax;
public bool TimeUpgradeMaxVis = false;
public GameObject ShieldUpgrade50;
public bool ShieldUpgrade50Vis = true;
public GameObject ShieldUpgrade200;
public bool ShieldUpgrade200Vis = false;
public GameObject ShieldUpgrade500;
public bool ShieldUpgrade500Vis = false;
public GameObject ShieldUpgrade1000;
public bool ShieldUpgrade1000Vis = false;
public GameObject ShieldUpgrade3000;
public bool ShieldUpgrade3000Vis = false;
public GameObject ShieldUpgrade5000;
public bool ShieldUpgrade5000Vis = false;
//public GameObject ShieldupgradeMax;
public bool ShieldUpgradeMaxVis = false;
public GameObject StarUpgrade50;
public bool StarUpgrade50Vis = true;
public GameObject StarUpgrade200;
public bool StarUpgrade200Vis = false;
public GameObject StarUpgrade500;
public bool StarUpgrade500Vis = false;
public GameObject StarUpgrade1000;
public bool StarUpgrade1000Vis = false;
public GameObject StarUpgrade3000;
public bool StarUpgrade3000Vis = false;
public GameObject StarUpgrade5000;
public bool StarUpgrade5000Vis = false;
//public GameObject StarUpgradeMax;
public bool StarUpgradeMaxVis = false;
private void Awake()
{
if (shopUIManager == null)
{
shopUIManager = this;
}
else if (shopUIManager != this)
{
Destroy(gameObject);
}
}
private void Start()
{
shopCoins = shopCoins + CoinsManager.coinsManager.shopCoins;
shopCoinsText.text = "" + shopCoins;
Load();
TimeStatus();
ShieldStatus();
StarStatus();
}
private void OnDisable()
{
Save();
}
private void Update()
{
CoinsManager.coinsManager.shopCoins = shopCoins;
}
public void StarStatus()
{
if (StarUpgrade200Vis == true)
{
StarUpgrade50.SetActive(false);
StarUpgrade200.SetActive(true);
}
if (ShieldUpgrade500Vis == true)
{
StarUpgrade50.SetActive(false);
StarUpgrade500.SetActive(true);
}
if (ShieldUpgrade1000Vis == true)
{
StarUpgrade50.SetActive(false);
StarUpgrade1000.SetActive(true);
}
if (ShieldUpgrade3000Vis == true)
{
StarUpgrade50.SetActive(false);
StarUpgrade3000.SetActive(true);
}
if (ShieldUpgrade5000Vis == true)
{
StarUpgrade50.SetActive(false);
StarUpgrade5000.SetActive(true);
}
if (StarUpgradeMaxVis == true)
{
StarUpgrade50.SetActive(false);
//StarUpgradeMax.SetActive(true);
}
}
public void ShieldStatus()
{
if (ShieldUpgrade200Vis == true)
{
ShieldUpgrade50.SetActive(false);
ShieldUpgrade200.SetActive(true);
}
if (ShieldUpgrade500Vis == true)
{
ShieldUpgrade50.SetActive(false);
ShieldUpgrade500.SetActive(true);
}
if (ShieldUpgrade1000Vis == true)
{
ShieldUpgrade50.SetActive(false);
ShieldUpgrade1000.SetActive(true);
}
if (ShieldUpgrade3000Vis == true)
{
ShieldUpgrade50.SetActive(false);
ShieldUpgrade3000.SetActive(true);
}
if (ShieldUpgrade5000Vis == true)
{
ShieldUpgrade50.SetActive(false);
ShieldUpgrade5000.SetActive(true);
}
if (ShieldUpgradeMaxVis == true)
{
ShieldUpgrade50.SetActive(false);
//ShieldUpgradeMax.SetActive(true);
}
}
public void TimeStatus()
{
if (TimeUpgrade200Vis == true)
{
TimeUpgrade50.SetActive(false);
TimeUpgrade200.SetActive(true);
}
if (TimeUpgrade500Vis == true)
{
TimeUpgrade50.SetActive(false);
TimeUpgrade500.SetActive(true);
}
if (TimeUpgrade1000Vis == true)
{
TimeUpgrade50.SetActive(false);
TimeUpgrade1000.SetActive(true);
}
if (TimeUpgrade3000Vis == true)
{
TimeUpgrade50.SetActive(false);
TimeUpgrade3000.SetActive(true);
}
if (TimeUpgrade5000Vis == true)
{
TimeUpgrade50.SetActive(false);
TimeUpgrade5000.SetActive(true);
}
if (TimeUpgradeMaxVis == true)
{
TimeUpgrade50.SetActive(false);
//TimeUpgradeMax.SetActive(true);
}
}
public void Save()
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Create(Application.persistentDataPath + "/shop.dat");
ShopData data = new ShopData();
data.Time50Vis = TimeUpgrade50Vis;
data.Time200Vis = TimeUpgrade200Vis;
data.Time500Vis = TimeUpgrade500Vis;
data.Time1000Vis = TimeUpgrade1000Vis;
data.Time3000Vis = TimeUpgrade3000Vis;
data.Time5000Vis = TimeUpgrade5000Vis;
data.TimeMaxVis = TimeUpgradeMaxVis;
data.Shield50Vis = ShieldUpgrade50Vis;
data.Shield200Vis = ShieldUpgrade200Vis;
data.Shield500Vis = ShieldUpgrade500Vis;
data.Shield1000Vis = ShieldUpgrade1000Vis;
data.Shield3000Vis = ShieldUpgrade3000Vis;
data.Shield5000Vis = ShieldUpgrade5000Vis;
data.ShieldMaxVis = ShieldUpgradeMaxVis;
data.Star50Vis = StarUpgrade50Vis;
data.Star200Vis = StarUpgrade200Vis;
data.Star500Vis = StarUpgrade500Vis;
data.Star1000Vis = StarUpgrade1000Vis;
data.Star3000Vis = StarUpgrade3000Vis;
data.Star5000Vis = StarUpgrade5000Vis;
data.StarMaxVis = StarUpgradeMaxVis;
bf.Serialize(file, data);
file.Close();
}
public void Load()
{
if (File.Exists(Application.persistentDataPath + "/shop.dat"))
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(Application.persistentDataPath + "/shop.dat", FileMode.Open);
ShopData data = (ShopData)bf.Deserialize(file);
file.Close();
TimeUpgrade50Vis = data.Time50Vis;
TimeUpgrade200Vis = data.Time200Vis;
TimeUpgrade500Vis = data.Time500Vis;
TimeUpgrade1000Vis = data.Time1000Vis;
TimeUpgrade3000Vis = data.Time3000Vis;
TimeUpgrade5000Vis = data.Time5000Vis;
TimeUpgradeMaxVis = data.TimeMaxVis;
ShieldUpgrade50Vis = data.Shield50Vis;
ShieldUpgrade200Vis = data.Shield200Vis;
ShieldUpgrade500Vis = data.Shield500Vis;
ShieldUpgrade1000Vis = data.Shield1000Vis;
ShieldUpgrade3000Vis = data.Shield3000Vis;
ShieldUpgrade5000Vis = data.Shield5000Vis;
ShieldUpgradeMaxVis = data.ShieldMaxVis;
StarUpgrade50Vis = data.Star50Vis;
StarUpgrade200Vis = data.Star200Vis;
StarUpgrade500Vis = data.Star500Vis;
StarUpgrade1000Vis = data.Star1000Vis;
StarUpgrade3000Vis = data.Star3000Vis;
StarUpgrade5000Vis = data.Star5000Vis;
StarUpgradeMaxVis = data.StarMaxVis;
}
}
}
[Serializable] class ShopData { public bool Time50Vis = true; public bool Time200Vis = false; public bool Time500Vis = false; public bool Time1000Vis = false; public bool Time3000Vis = false; public bool Time5000Vis = false; public bool TimeMaxVis = false;
public bool Shield50Vis = true;
public bool Shield200Vis = false;
public bool Shield500Vis = false;
public bool Shield1000Vis = false;
public bool Shield3000Vis = false;
public bool Shield5000Vis = false;
public bool ShieldMaxVis = false;
public bool Star50Vis = true;
public bool Star200Vis = false;
public bool Star500Vis = false;
public bool Star1000Vis = false;
public bool Star3000Vis = false;
public bool Star5000Vis = false;
public bool StarMaxVis = false;
}
using System.Collections; using System.Collections.Generic; using UnityEngine; using System; using System.Runtime.Serialization.Formatters.Binary; using System.IO;
public class CoinsManager : MonoBehaviour {
public static CoinsManager coinsManager;
public int shopCoins = 0;
public int ninjaTimeIncrease = 0;
public int ninjaShieldIncrease = 0;
public int ninjaStarIncrease = 0;
//still need to do
public int coinMultiplier = 0;
public int scoreBump = 0;
private void Awake()
{
if (coinsManager == null)
{
DontDestroyOnLoad(gameObject);
coinsManager = this;
}
else if (coinsManager != this)
{
Destroy(gameObject);
}
Load();
}
private void OnDisable()
{
Save();
}
public void Save()
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Create(Application.persistentDataPath + "/coins.dat");
CoinsData data = new CoinsData();
data.coins = shopCoins;
data.timeIncrease = ninjaTimeIncrease;
data.shieldIncrease = ninjaShieldIncrease;
data.starIncrease = ninjaStarIncrease;
bf.Serialize(file, data);
file.Close();
}
public void Load()
{
if (File.Exists(Application.persistentDataPath + "/coins.dat"))
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(Application.persistentDataPath + "/coins.dat", FileMode.Open);
CoinsData data = (CoinsData)bf.Deserialize(file);
file.Close();
shopCoins = data.coins;
ninjaTimeIncrease = data.timeIncrease;
ninjaShieldIncrease = data.shieldIncrease;
ninjaStarIncrease = data.starIncrease;
}
}
}
//int in the coin manager to determine the length of time a powerup is active //int will be affected by the powerup purchase in the shop //since the coin manager is the place of saving for anything coin related it will also save //the powermethods since those are based on purchases and it will also save the //shop assumptions since the shop affects the coins
//player will need different coroutines to respond to the powerup int of the coinmanager //ShopUIManager will need different setactives and inactives on start() to respond to current power //up level //shopUIManager will save it's own data for what to setactive and inactive based on recent purchases //can also be done from the coin manager [Serializable] class CoinsData { public int coins; public int timeIncrease; public int shieldIncrease; public int starIncrease;
}