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 Akkels · Jan 13, 2016 at 06:24 PM · c#scripting problemserializationsavingdata

Variables changes even on pause

Hello, i have a script for storing serialized data. But some variables are always setting equal to each other. For example statLevels in progressData are always equal statLevelsStats in stats, even if game on pause in Unity.

Here is a code:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 using System.Runtime.Serialization.Formatters.Binary;
 using System.IO;
 
 [System.Serializable]
 public class ProgressContainer {
     [System.Serializable]
     public class progressData
     {
         public int lastCheckPoint;
         public int level;
         public List<int> pickedUpMonets = new List<int>();
         public bool notSaved = true;
         public int coins;
         public int skates;
         public int[] statLevels = new int[4];
         public int[] skillLevels = new int[4];
     }
 
     [System.Serializable]
     public class Settings
     {
         public bool watchedYYYC; 
         public bool helpersHidden;
         public float audioMusicVolume = 1;
         public float audioSoundsVolume = 1;
         public bool mute;
         public Language language;
     }
 
     [System.Serializable]
     public class Stats
     {
         public bool watchedDialog = false;
         public int coins;
         public int skates;
         public int energy;
         public int lastLevel;
         public int lastOpenedLevel = -1;
         public int[] skillLevels = new int[4];
         public int[] statLevelsStats = new int[4];
         public bool hardcore;
         public int lives = 5;
         public bool gameStarted = false;
     }
 
     public List<progressData> progress = new List<progressData>();
     public Stats stats = new Stats();
     public Settings settings = new Settings();
     // Use this for initialization
     public void Save (string path) {
         BinaryFormatter bf = new BinaryFormatter ();
         
         path += "/playerInfo.dat";
         
         
         FileStream file = File.Create (path);
         bf.Serialize (file, this);
         file.Close ();
         Debug.Log ("Saved : "+path);
         
     }
     
     public static ProgressContainer Load(string path)
     {
         
         
         path += "/playerInfo.dat";
         if (!File.Exists (path))
             return new ProgressContainer();
         BinaryFormatter bf = new BinaryFormatter ();
         FileStream file = File.Open (path,FileMode.Open);
         
         ProgressContainer container;
         
         container = (ProgressContainer)bf.Deserialize (file);
         file.Close ();
         return container;
     }
     public void NewGame(bool hardcore)
     {
         progress.Clear();
         stats = new Stats ();
         stats.hardcore = hardcore;
         stats.gameStarted = true;
         if(Game.instance.unlockAllLevels)
         {
             stats.lastLevel = 99;
             stats.lastOpenedLevel = 99;
         }
         Game.instance.Save ();
     }
     
 }
 

Can it be because of Serialization? I Marked all functions in other scripts where changing variables in ProgressContainer with Debug.Log(). But they are not Calling.

Comment
Add comment · Show 1
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 ThomLaurent · Sep 14, 2016 at 01:18 PM 0
Share

It may not be the solution, but I note you use List<OneOfYourClasses> as if it was already a serializable type, whereas it's not. Remember the generic class List<> is only serializable with built-in types as a generic parameter (scalars, i.e int, bool, float, double, string, ...) not your serializable class, so for example List<int> is serializable but List<progressData> is not, you have to create a derived class from your special List to make it serializable: [System.Serializable] public class ProgressDataList : List<progressData> { } And declare the field like this: public ProgressDataList progress = new ProgressDataList();

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

65 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

Related Questions

How to save many instantiated objects position/rotation at runtime??? 2 Answers

how is it possible that my List has count = 3, but after im trying to compare onClick obhects name it says that my list is empty 0 Answers

Saving monster class List problem. 0 Answers

Best way to serialize a big array of a class 2 Answers

Saving Data (please help) 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