- Home /
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.buyTheThing () (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 buyTheThing()
{
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>(hile, "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 this 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.
Your answer
Follow this Question
Related Questions
Get Asset at runtime by its ID 0 Answers
Save and load serialization problem 0 Answers
Android, How can I permanently save data? (A couple variables) 4 Answers
SerializationException: Type UnityEngine.Vector3 is not marked as Serializable. 1 Answer
How do i change my save "Total Coin" everytime i get more coins. 0 Answers