So I wrote a custom saver, seen below. It's very basic since I'm new to serialization in general.
Code: Select all
public class CustomSaver : Saver
{
bool enemyActive;
string serializedEnemyActive;
int testmessage;
public override string RecordData()
{
/// This method should return a string that represents the data you want to save.
/// You can use SaveSystem.Serialize() to serialize a serializable object to a
/// string. This will use the serializer component on the Save System GameObject,
/// which defaults to JSON serialization.
enemyActive = GetComponent<EnemyAI>().Active;
serializedEnemyActive = SaveSystem.Serialize(enemyActive);
return serializedEnemyActive;
//testmessage = GetComponent<EnemyAI>().testLoad;
//serializedEnemyActive = SaveSystem.Serialize(testmessage);
//return serializedEnemyActive;
}
public override void ApplyData(string data)
{
/// This method should process the string representation of saved data and apply
/// it to the current state of the game. You can use SaveSystem.Deserialize()
/// to deserialize the string to an object that specifies the state to apply to
/// the game.
GetComponent<EnemyAI>().Active = SaveSystem.Deserialize<bool>(serializedEnemyActive);
//GetComponent<EnemyAI>().testLoad = SaveSystem.Deserialize<int>(serializedEnemyActive);
}
}
"NullReferenceException: Object reference not set to an instance of an object
UnityEngine.JsonUtility.FromJson[T] (System.String json) (at <1386288601af43018501cce2912f52f4>:0)
PixelCrushers.JsonDataSerializer.Deserialize[T] (System.String s, T data) (at <810d2e6289844e9fbf86de0ff4e5efbb>:0)
PixelCrushers.SaveSystem.Deserialize[T] (System.String s, T data) (at <810d2e6289844e9fbf86de0ff4e5efbb>:0)
PixelCrushers.CustomSaver.ApplyData (System.String data) (at Assets/Scripts/CustomSaver.cs:35)
PixelCrushers.SaveSystem.ApplySavedGameData (PixelCrushers.SavedGameData savedGameData) (at <810d2e6289844e9fbf86de0ff4e5efbb>:0)
UnityEngine.Debug:LogException(Exception)"
Line 35 of the custom saver is the line where it deserializes the data. So at first I thought that was the issue. But after some sleuthing and testing, I ended up writing a debug message that prints the variable "serializedEnemyActive" after the data's been serialized. The string I got was only "{}". I tested this with the testvariable as well and got the same result. I know little to nothing about serialization, but if it's saved as a unique string, I feel as though it shouldn't be printed as a pair of empty brackets.
Regardless, I've also tested the serialization with the gameobject's position and a completely different gameobject's position, and I still get the error. I have no idea where this error is coming from; to my knowledge the above code is exactly the same as it was previously. I also followed the documentation again to make sure all the needed components were in the scene. So... what's going on here?