How to fix SerializationException?

Error text: Type ‘UnityEngine.GameObject’ in Assembly ‘UnityEngine.CoreModule, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null’ is not marked as serializable.
System.Runtime.Serialization.FormatterServices.InternalGetSerializableMembers (System.RuntimeType type) (at <599589bf4ce248909b8a14cbe4a2034e>:0)

1 Script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
public class Save : MonoBehaviour
{
    public static void SaveData()
    {
        BinaryFormatter bf = new BinaryFormatter();
        FileStream file = File.Create(Application.persistentDataPath + "/saveGame.gd");
        bf.Serialize(file, GlobalNames.playerGO);
        file.Close();
    }
}

2 script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class GlobalNames : MonoBehaviour
{
    public static GameObject playerGO;
    void Awake()
    {
        playerGO = GameObject.FindWithTag("Player");
    }
}

3 script:

void Update()
{
        if(Input.GetKey(KeyCode.I))
        {
            Save.SaveData();
            Debug.Log("Сохранение");
        }
}

I press “I” and get an error. Help

I also get stuck the similar exception (SerializationException ) at runtime.

Here is my screenshot and scripts (which is zipped in a zip file)


9188789–1280594–Script.7z (12.6 KB)

Please don’t hijack/necro threads, simply create your own thread and ask your own questions.

Thanks.

1 Like