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 02, 2015 at 07:27 PM · c#serializationsaveload

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

Thanks in advance .I can't seem to get my Save/Load script to work :(. I try to use Unity serialization for this ,but alas when I hit the Load button I strangely get the values that my Game Master had in the beginning, before I even made this script!?

Notes:

  • This script is attached to Game Master object.

  • This object travels between scenes using a singleton design pattern.

  • Please don't tell me to use Player Prefs, I plan to add more variables and I want to learn !!

  • Sorry, new to Unity Answers don't know how to post my script properly :/.

  • I have tried both using File.Create and File.Open for my save method, same result.

  • I have found the file on my computer, so the the save method does create one.

using UnityEngine; using System.Collections.Generic; 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(){
 
 
         if (File.Exists (Application.persistentDataPath + "GameInfo.dat")) {
             BinaryFormatter bf = new BinaryFormatter ();
             FileStream file = File.Open(Application.persistentDataPath + "GameInfo.dat", FileMode.Open);
             Debug.Log (Application.persistentDataPath);
 
             PlayerData data = new PlayerData ();
             data.Armour = Armour;
             data.Credits = 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.Armour;
             Credits = data.Credits;
 
 
             }
 
     }
 
     [System.Serializable]
     public class PlayerData{
         public float Armour;
         public int Credits;
 
     }
 
 }






Comment
Add comment · Show 4
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 Cherno · Aug 02, 2015 at 07:39 PM 1
Share

Have yu tried inserting Debug.Log lines to see if the right values are saved and loaded inside the save and load function? Your script looks good to me at quick glance.

avatar image Garross · Aug 02, 2015 at 08:03 PM 0
Share

Great Idea, thanks!; I have noticed that when I save it ignores my current values and sets the data values to serialized to 6 and 20!!!!! Why could this be?

avatar image Cherno · Aug 02, 2015 at 08:17 PM 1
Share

I wouldn't say that it "ignores the current values"; rather, it in fact takes the Armour and Credits values, but these are for some reason 6 and 20 (you can check this with more Debug.Log lines if it isn't apparent in the inspector). You probably set these variables to 6 and 20 before saving somewhere.

avatar image Garross · Aug 02, 2015 at 08:26 PM 0
Share

I just did...and it sees them correctly(also they show up in the inspector),but when I save ,it saves the base values only, i have now changed them to 0 and 0.

3 Replies

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

Answer by Garross · Aug 03, 2015 at 01:44 PM

Figured it out xD my save button was attached to a prefab of my Game Master !!!! Thank you all so much for your help and setting me on the right track.

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
avatar image
1

Answer by Positive7 · Aug 02, 2015 at 07:39 PM

 Application.persistentDataPath + "GameInfo.dat"

to

 Application.persistentDataPath + "/GameInfo.dat"


Comment
Add comment · Show 3 · 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
avatar image Garross · Aug 02, 2015 at 07:55 PM 0
Share

Thanks ,however now that i have changed the script the Save/Load is working very strangely.... when I save my game I have 750 credits , when I load I have 20(a value that was used in the script previously to test the UI counter)????As well as that when I go back to menu from my Level 1 without saving , the load button does absolutely nothing, doesn't even call my Debug Lines?!

Update: I have debug.Logged my save values and I have noticed that for some wild reason it saves the data variables to be stored as 20 and 6 totally ignoring my current values from the game!

 using System.Collections.Generic;
 using System.Linq;
 using UnityEngine.UI;
 using System.Collections;
 using System;
 using System.Runtime.Serialization.Formatters.Binary;
 using System.IO;
 
 
 public class SaveLoad : $$anonymous$$onoBehaviour {
 
     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 $$anonymous$$illPlayer(Player player){
 
         Application.LoadLevel("$$anonymous$$enu");
 
 
         
     }
     
     public static void $$anonymous$$illEnemy(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.Armour = Armour;
             data.Credits = 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", File$$anonymous$$ode.Open);
             PlayerData data = (PlayerData)bf.Deserialize(file);
             Debug.Log ("Load");
             file.Close();
             Armour = data.Armour;
             Credits = data.Credits;
 
 
             }
 
     }
 
     [System.Serializable]
     public class PlayerData{
         public float Armour;
         public int Credits;
 
     }
 
 }

avatar image Positive7 · Aug 02, 2015 at 08:02 PM 1
Share

Weird it works fine here. $$anonymous$$aybe the error is somewhere else.

avatar image Garross · Aug 02, 2015 at 08:16 PM 0
Share

I have discovered something new ,by putting some Debug.Logs into my update function xD. $$anonymous$$y save file for some reason does not take the new values of my variables but ins$$anonymous$$d uses the ones I had in the beginning of the scene,therefore when I load I get my base/original values!!!!!! :O soooo confused! .. Thank you all for your help by the way!

avatar image
0

Answer by Voxel-Busters · Aug 08, 2015 at 03:04 AM

You can save all the trouble, by directly serializing MonoBehaviour using our plugin Runtime Serialization for Unity. Its not just another serialization plugin which works only on custom c# objects. But what makes it special is its capablity to serialize Unity Objects like GameObject, MonoBehaviours, Textures, Prefabs etc. As a matter of fact, you can even use it for Scene Serialization. For more info about supported list, please check this link.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Save and load serialization problem 0 Answers

My save function saves the wrong values!! 1 Answer

Coding help: How to use xml serialization 1 Answer

Error serializing a class to save / load 2 Answers

C# Issues with either PlayerPrefs.SetVariable or UnityEvent.Invoke 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