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 05, 2018 at 07:23 PM · saveloadbinaryformatter

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

I'm not sure that i've phrased this question properly but I hope this clarifies. I will post the three referenced codes in this order; "DataManger," (which also has the private class inside it PlayerData which is serializable) "Start_Screen_UIManager," and "ScoreManager"

I'm working on a save and load funtion.

DataManager Exists in the intital load of the entire game and then has a don't destroy on load funtion so that it can communicate with a bunch of other stuff later on.

Start_Screen_UIManager, exists in the first Scene but not in the second scene so that the UI Elements (buttons, images etc) don't persist into the second scene.

ScoreManager is a prefab which is in both the first and second scene but in the awake function will destroy itself if there is already another instance of it (in case while i'm editing I decide to start out in the second scene)

Here's how it should work. ScoreManager in gameplay will acquire (from an unreference script and gameObject) the newest high score and post that to the Start_Screen_UIManger (this part works) and will replace that highscore if another score is higher on later gameplays (also works). Where it goes wrong, is when I try to implement the DataManager.

I watched the older live training session on data persistence and the typing of the code works great and comes back with no errors when applied to my scripts and gameObjects).

I have the Save() Method being called OnDisable() of the DataManager and the Load() Method being call Awake() of the of the DataManager. So it should load. I would expect it to have nothing to load on my first play but when I stop the game and start it again it says the object i'm referencing is improperly referenced (line 58 of the DataManager or if the lines don't populate that last line of Data Manager before the "}".

All three objects holding these scripts are available in the first scene when it loads. Here are my scripts, please let me know what I'm not understanding or how I can better reference the GameObject holding the script.

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

public class DataManager : MonoBehaviour {

 public static DataManager dataManager;

 public void Awake()
 {
     {
         if (dataManager == null)
         {
             DontDestroyOnLoad(gameObject);
             dataManager = this;
         }
         else if (dataManager != null)
         {
             Destroy(gameObject);
         }
     }

     Load();
 }

 
 public void OnDisable()
 {
     Save();
 }

 public void Save()
 {
     BinaryFormatter bf = new BinaryFormatter();
     FileStream file = File.Create(Application.persistentDataPath + "/playerInfo.dat");

     PlayerData data = new PlayerData();
     data.score = Start_Screen_UIManager.ssUIManager.savedHighScore;
     Load() not calling the the binaryformatter = file.create

     bf.Serialize(file, data);
     file.Close();
 }

 public void Load()
 {
     if(File.Exists(Application.persistentDataPath + "/playerInfo.dat"))
     {
         BinaryFormatter bf = new BinaryFormatter();
         FileStream file = File.Open(Application.persistentDataPath + "/playerInfo.dat", FileMode.Open);
         PlayerData data = (PlayerData)bf.Deserialize(file);
         file.Close();

         Start_Screen_UIManager.ssUIManager.savedHighScore = data.score;
        
     }
 }

}

[Serializable] class PlayerData { public int score;

}

using System.Collections; using System.Collections.Generic; using UnityEngine.UI; using UnityEngine;

public class Start_Screen_UIManager : MonoBehaviour {

 public static Start_Screen_UIManager ssUIManager;



 public Text savedHighScoreText;
 public int savedHighScore = 0;
 

 private void Awake()
 {
  
     if (ssUIManager == null)
     {
         
         ssUIManager = this;
     }

     else if (ssUIManager != this)
     {
         Destroy(gameObject);
     }
 }

 

 public void Update()
 {
     if (savedHighScore < ScoreManager.scoreManager.newHighScore)
     {
         savedHighScore = ScoreManager.scoreManager.newHighScore;
         savedHighScoreText.text = "High Score: " + savedHighScore;
     }
     else
     {
         savedHighScoreText.text = "High Score: " + savedHighScore;
     }

 }

}

using System.Collections; using System.Collections.Generic; using UnityEngine.SceneManagement; using UnityEngine;

public class ScoreManager : MonoBehaviour {

 public static ScoreManager scoreManager;

 public int newHighScore = 0;

 

 private void Awake()
 {

     if (scoreManager == null)
     {
         DontDestroyOnLoad(gameObject);
         scoreManager = this;
     }

     else if (scoreManager != this)
     {
         Destroy(gameObject);
     }

 }

 


 private void Update()
 {
     Scene currentScene = SceneManager.GetActiveScene();
     string sceneName = currentScene.name; 

     if (sceneName != "Play_Scene")
     {
         return;
     }
     else if (sceneName == "Play_Scene")
     {
         if (newHighScore < UIManager.uIManager.newHighScore)
         {
             newHighScore = UIManager.uIManager.newHighScore;
         }
     }
 }



}

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

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

Build not working on some saves for binary formatter but works on others 0 Answers

Save and Load in the build 0 Answers

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

Saving and loading data from file 0 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