Either SaveSystem.Serialize or SaveSystem.Deserialize are no longer working.
Posted: Mon Feb 15, 2021 10:52 pm
Hello,
So I wrote a custom saver, seen below. It's very basic since I'm new to serialization in general.
Now, I tested this with the "testmessage" variable, working basically the same as the enemyActive variable. When I did, it worked. But that was a week ago, and during this week an unrelated incident happened where the master branch of our project got some serious issues. As a result, we found it best to create a new repository using an older version of the project one of our members had. Because my scripts had nothing to do with the issue, I exported them as a unity package (including the custom saver) and reimported them when the new project was on my system. However, after briefly recreating/replacing other things that had been lost and running the game to make sure the save system was running, this error message appeared whenever I loaded a saved slot:
"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?
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?