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 /
  • Help Room /
avatar image
0
Question by Refar8 · Feb 04, 2019 at 03:17 PM · serializationsaveloadsave datasaveload

problem with loading data with serialization in the right time

Hello all,

I made a save system and it works fine but the problem is first health and other data loading then scene is loading so in the new scene my character can`t get the right data. I want to know should I make it work? here is the codes, a collider trigger active the saveData() then it interact with gameManager to save and then a button can load later.

 public class saveLoadData : MonoBehaviour {
 
     gameManager theGM;
 
     // Use this for initialization
     void Awake () {
         theGM = gameManager.instance;
     }
     
     // Update is called once per frame
     void Update () {
 
     }
 
     public void saveData()
     {
         if (!Directory.Exists(Application.dataPath + "SavedGames"))
             Directory.CreateDirectory(Application.dataPath + "/SavedGames");
 
         BinaryFormatter bf = new BinaryFormatter();
 
         FileStream fs = File.Create(Application.dataPath + "/SavedGames/save.db");
 
         saveManager saveManager = new saveManager();
 
         saveManager.currentLevel = theGM.getCurrentLevel();
         saveManager.currentHealth = theGM.getCurrentHealth();
         saveManager.currentRounds = theGM.getCurrentRounds();
 
         bf.Serialize(fs, saveManager);
 
         Debug.Log("Game Saved!");
 
         fs.Close();       
     }
 
     public void loadData()
     {
         if(File.Exists(Application.dataPath + "/SavedGames/save.db"))
         {
             BinaryFormatter bf = new BinaryFormatter();
 
             FileStream fs = File.Open(Application.dataPath + "/SavedGames/save.db", FileMode.Open);
 
             saveManager saveManager = (saveManager)bf.Deserialize(fs);
 
             fs.Close();
 
             theGM.loadLastLevel(saveManager.currentLevel);
             theGM.loadLastHealth(saveManager.currentHealth);
             theGM.loadLastRounds(saveManager.currentRounds);     
         }
         if(!File.Exists(Application.dataPath + "/SavedGames/save.db"))
         {
             Debug.Log("No saved file present!");
         }
     }
 }
 
 [System.Serializable]
 class saveManager
 {
     public int currentLevel;
     public float currentHealth;
     public int currentRounds;
 }

and here is gameManager:

public class gameManager : MonoBehaviour {

 public static gameManager instance;

 playerController myPC;
 playerHealth myPH;

 // Use this for initialization
 void Awake () {
     if(instance == null)
     {
         instance = this;
     }
     else if(instance != this)
     {
         Destroy(gameObject);
     }
 }

 void Start()
 {
     myPC = GameObject.FindGameObjectWithTag("Player").GetComponent<playerController>();
     myPH = GameObject.FindGameObjectWithTag("Player").GetComponent<playerHealth>();
 }

 // Update is called once per frame
 void Update () {
     
 }

 //loading data
 public void loadLastLevel(int x)
 {
     SceneManager.LoadScene(x);
 }

 public void loadLastHealth(float h)
 {
     myPH.currentHealth = h;
     myPH.healthSlider.value = h;
 }

 public void loadLastRounds(int r)
 {
     myPC.remainingRounds = r;
 }

 //getting data
 public int getCurrentLevel()
 {
     return SceneManager.GetActiveScene().buildIndex;
 }

 public float getCurrentHealth()
 {
     return myPH.currentHealth;
 }

 public int getCurrentRounds()
 {
     return myPC.remainingRounds;
 }

}

I am really appreciate for any help

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

172 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 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

How do i add coins in my Save Total Coin, everytime i collect more coins in the game? 0 Answers

How to save money 1 Answer

Is it normal that saving data to file makes game freeze for a long time? 1 Answer

How can I save my options menu? 0 Answers

How to make a save script in javascript. 1 Answer


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