• 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 alpergktepe · Apr 14, 2020 at 02:31 PM · serializationsavesave datasaveload

Error while saving game: SerializationException: Type 'UnityEngine.GameObject' in Assembly 'UnityEngine.CoreModule, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable.

I've made a generic save system that you can save datas with keys and then load them back again. It was working fine but when I tried to save two lists it gave me the following errors:

 SerializationException: End of Stream encountered before parsing was completed.
 ...
 SaveSystemF.Load[T] (System.String key) (at Assets/Scripts/SaveSystem/SaveSystemF.cs:33)
 CollectableDiseases.LoadHastaliklar () (at Assets/Scripts/Wanderers/CollectableDiseases.cs:45)
 CollectableDiseases.Start () (at Assets/Scripts/Wanderers/CollectableDiseases.cs:19)

and:

 SerializationException: Type 'UnityEngine.GameObject' in Assembly 'UnityEngine.CoreModule, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable.
 
 SaveSystemF.Save[T] (T saveObject, System.String key) (at Assets/Scripts/SaveSystem/SaveSystemF.cs:19)
 randomBuy.Save () (at Assets/Scripts/randomBuy.cs:93)
 GameEvents.onSaveInitiated () (at Assets/Scripts/SaveSystem/GameEvents.cs:23)
 randomBuy.buyTheT$$anonymous$$ng () (at Assets/Scripts/randomBuy.cs:79)
 UnityEngine.Events.InvokableCall.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent/UnityEvent.cs:166)
 UnityEngine.Events.UnityEvent.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent/UnityEvent/UnityEvent_0.cs:58)
 UnityEngine.UI.Button.Press () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:66)
 UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:108)
 UnityEngine.EventSystems.ExecuteEvents.Execute (UnityEngine.EventSystems.IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:50)
 UnityEngine.EventSystems.ExecuteEvents.Execute[T] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.ExecuteEvents+EventFunction`1[T1] functor) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:261)
 UnityEngine.EventSystems.EventSystem:Update()

Here is the script where I try to save the list:

 public class randomBuy : MonoBehaviour
 {
 
     public int paraOyuncu;
     public int paraFiyat;
     int gelenPara;
 
     [SerializeField]
     public List<GameObject> hastaliklar = new List<GameObject>();
     public List<GameObject> yakalanmisHastaliklar = new List<GameObject>();
 
 
     CollectableDiseases hastalikci = null;
 
     void Start()
     {
         hastalikci = GameObject.Find("CD").GetComponent<CollectableDiseases>();
 
         hastaliklar = hastalikci.hastaliklar;
         yakalanmisHastaliklar = hastalikci.yakalanmisHastaliklar;
 
         GameEvents.SaveInitiated += Save;
 
         LoadMoney();
         LoadGelenPara();
         LoadBox();
 
         paraOyuncu += gelenPara;
     }
 
     public void buyTheT$$anonymous$$ng()
     {
         if(paraOyuncu >= paraFiyat && hastalikci.hastaliklar.Count != 0)
         {
             //ürünü al
 
             paraOyuncu -= paraFiyat;
             paraFiyat += zamMiktarı;
 
             GameEvents.onSaveInitiated();
         }
     }
     void Save()
     {
         SaveSystemF.Save<int>(paraOyuncu, "Player Money");
         SaveSystemF.Save<int>(paraFiyat, "Box Price");
         SaveSystemF.Save<int>($$anonymous$$le, "Player New Money");
         SaveSystemF.Save<List<GameObject>>(hastaliklar, "Hastaliklar");
         SaveSystemF.Save<List<GameObject>>(yakalanmisHastaliklar, "Yakalanmis Hastaliklar");
     }
 
     void LoadMoney()
     {
         if(SaveSystemF.SaveExists("Player Money"))
         {
             paraOyuncu = SaveSystemF.Load<int>("Player Money");
         }
     }
 
     void LoadBox()
     {
         if(SaveSystemF.SaveExists("Box Price"))
         {
             paraFiyat = SaveSystemF.Load<int>("Box Price");
         }
     }
 
     void LoadGelenPara()
     {
         if (SaveSystemF.SaveExists("Player New Money"))
         {
             gelenPara = SaveSystemF.Load<int>("Player New Money");
         }
     }
 }

and t$$anonymous$$s is the script where I try to load:

 public class CollectableDiseases : MonoBehaviour
 {
     [SerializeField]
     public List<GameObject> hastaliklar = new List<GameObject>();
     public List<GameObject> yakalanmisHastaliklar = new List<GameObject>();
 
     void Start()
     {
         LoadHastaliklar();
         LoadYakalanmisHastaliklar();
     }
 
     void LoadHastaliklar()
     {
         if (SaveSystemF.SaveExists("Hastaliklar"))
         {
             hastaliklar = SaveSystemF.Load<List<GameObject>>("Hastaliklar");
         }
     }
 
 
     void LoadYakalanmisHastaliklar()
     {
         if (SaveSystemF.SaveExists("Yakalanmis Hastaliklar"))
         {
             yakalanmisHastaliklar = SaveSystemF.Load<List<GameObject>>("Yakalanmis Hastaliklar");
         }
     }
 }

also my saving script:

 public class SaveSystemF : MonoBehaviour
 {
     public static void Save<T>(T saveObject, string key)
     {
         string path = Application.persistentDataPath + "/saves/";
         Directory.CreateDirectory(path);
 
         BinaryFormatter formatter = new BinaryFormatter();
 
         using (FileStream fileStream = new FileStream(path + key + ".txt", FileMode.Create))
         {
             formatter.Serialize(fileStream, saveObject);
         }
     }
 
     public static T Load<T>(string key)
     {
         string path = Application.persistentDataPath + "/saves/";
 
         T returnValue = default(T);
 
         BinaryFormatter formatter = new BinaryFormatter();
 
         using (FileStream fileStream = new FileStream(path + key + ".txt", FileMode.Open))
         {
             returnValue =  (T)formatter.Deserialize(fileStream);
         }
 
         return returnValue;
     }
 
     public static bool SaveExists(string key)
     {
         string path = Application.persistentDataPath + "/saves/" + key + ".txt";
 
         return File.Exists(path);
     }
 }

and:

 public class GameEvents : MonoBehaviour
 {
     public static System.Action SaveInitiated;
 
 
     void Start()
     {
         
     }
 
     // Update is called once per frame
     void Update()
     {
         
     }
 
     public static void onSaveInitiated()
     {
         SaveInitiated?.Invoke();
     }
 }

I deleted the unneccesary code between, hope you can help me. I've read lots of Q&A's but they did not solve my problem. It works for other values: paraOyuncu and paraFiyat.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

136 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

Related Questions

Saving progress even when I quit the windows app 0 Answers

Save and load system not working please help 1 Answer

How to make a list of saves and load them correctly 0 Answers

saving array to bin file 0 Answers

How do I make a basic easy Save and load function with 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