Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by surfuay · Dec 10, 2018 at 02:17 AM · saveloadbinaryformatter

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;

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
â–¼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

98 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Which types are actually serializable? Is the documentation incorrect or am I? 3 Answers

Load() not calling the the binaryformatter = file.create 0 Answers

Is there a list of types binaryformatter can save? 2 Answers

Save and Load in the build 0 Answers

Save and Load 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges