Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 Garross · Aug 03, 2015 at 05:29 PM · c#serializationsaveload

My save function saves the wrong values!!

Thanks in advance. My save/load script is attached to game master object/prefab which travels between scenes , the load function works ,however my save function only saves the base values of my object.

e.g if i set my game master's prefab Armour and Credits value are set to 6 and 20 then the script only ever saves those values and never the updated values of Armour and Credits(I can see the updated values in the inspector when I play). What could be the root of this mystery?

using System.Linq; using UnityEngine.UI; using System.Collections; using System; using System.Runtime.Serialization.Formatters.Binary; using System.IO;

 public class SaveLoad : MonoBehaviour {
 
     public static SaveLoad control;
 
     public float Armour;
     public int Credits;
     public Text Score;
 
 
 
     void Awake(){
         if (control == null) {
             control = this;
             DontDestroyOnLoad (gameObject);
         } else if (control != this) {
             Destroy(gameObject);
         }
     }
 
     void Update(){
 
         if (Score == null) {
             Debug.Log("No Score Counter!!");
         }
         
     control.Score.text ="Credits: " +Credits.ToString();
 
 
     }
 
     public static void KillPlayer(Player player){
 
         Application.LoadLevel("Menu");
 
 
         
     }
     
     public static void KillEnemy(Enemy Enemy){
         Destroy (Enemy.gameObject);
         
     }
 
     public void Save(){
 
 
 
             BinaryFormatter bf = new BinaryFormatter ();
             FileStream file = File.Create(Application.persistentDataPath + "/GameInfo.dat");
             Debug.Log (Application.persistentDataPath);
 
             PlayerData data = new PlayerData ();
             data.A = Armour;
             Debug.Log (data.A);
             Debug.Log (Armour);
             data.C = Credits;
             Debug.Log (data.C);
             Debug.Log (Credits);
             bf.Serialize (file, data);
 
             file.Close ();
             Debug.Log ("Save");
         }
 
         
     public void Load(){
         Debug.Log (Application.persistentDataPath);
         if(File.Exists(Application.persistentDataPath + "/GameInfo.dat")){
             BinaryFormatter bf = new BinaryFormatter();
             FileStream file = File.Open(Application.persistentDataPath + "/GameInfo.dat", FileMode.Open);
             PlayerData data = (PlayerData)bf.Deserialize(file);
             Debug.Log ("Load");
             file.Close();
             Armour = data.A;
             Debug.Log (data.A);
             Debug.Log (Armour);
             Credits = data.C;
             Debug.Log (data.A);
             Debug.Log (Credits);
 
 
             }
 
     }
 
     [System.Serializable]
     public class PlayerData{
         public float A;
         public int C;
 
     }
 
 }
Comment
Add comment · Show 2
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
avatar image LawgiverSS2 · Aug 04, 2015 at 04:34 AM 0
Share

How do you know your load function works if only the base values are saved?

Can you show us the output of your Debug.Log statements?

avatar image HarshadK · Aug 04, 2015 at 05:36 AM 0
Share

Where are you calling your Save and Load methods?

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Garross · Aug 04, 2015 at 08:46 AM

Solved it , my save button was set to my game master script/prefab not the in game object holding the variables xD i'm soo stupid!

Comment
Add comment · Share
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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Error serializing a class to save / load 2 Answers

Save and load serialization problem 0 Answers

Can't get serialization Load/Save to work!C# 3 Answers

Coding help: How to use xml serialization 1 Answer

Unity Serializer scripting error 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