SerializationException started popping up for me

I started having a random error after a unity restart. I think it has something to do with the save system and for now i skipped the " under the hood" section of the save system. also if it matters i have not refactored for the vulnerability yet. below is my error.

when i select it it brings me to line where we instantiate the persistentObject
( the error does not stop me from compiling or playing the game)

using System;
using UnityEngine;

namespace RPG.Core
{
    public class PeristentObjectSpawner : MonoBehaviour
    {
        [SerializeField] GameObject persistentObjectPrefab;

        static bool hasSpawned = false;

        private void Awake()
        {

            //i actually kindof hate this solution it feels a race condition risk all to protect against singleton...maybe refactor later
            if (hasSpawned) return;

            SpawnPersistentObjects();

            hasSpawned = true;
        }

        private void SpawnPersistentObjects()
        {
            GameObject persistentObject = Instantiate(persistentObjectPrefab);
            DontDestroyOnLoad(persistentObject);
        }
    }
}

NVM figured it out. it was the save file. just needed to delete it. i guess ill leave this up incase anyone else runs into the same problem.

Most often, this is caused by passing a value to CaptureState that cannot be Serialized… a LazyValue is a great example of this. LazyValue cannot be serialized by BinaryFormatter (or by my Json Saving System, for that matter). What we need to do when dealing with LazyValues is to return the .value of the LazyValue
Example:

public object CaptureState()
{
    return currentHeatlh.value;
}
public void RestoreState(object state)
{
    currentHealth.value = (float)state;
}

Once the CaptureState encounters an object it cannot serialize, it fails and leaves the save file in a state that cannot be deserialized.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms