I am saving a list of ScriptableObject
s in a JSON file using NewtonSoft JSON, the problem being that the following error message is thrown when it tries to save:
NotSupportedException: rigidbody property has been deprecated(wrapper dynamic-method) UnityEngine.GameObject.Getrigidbody(object)
My code for saving and loading:
private static readonly string SaveFileFolder = Application.persistentDataPath + "/Saves/";
public static void Save(SaveData data)
{
if(Directory.Exists(SaveFileFolder) == false)
{
Directory.CreateDirectory(SaveFileFolder);
}
File.WriteAllText(SaveFileFolder + "Data.sav", JsonConvert.SerializeObject(data));
}
public static SaveData Load()
{
string _savePath = SaveFileFolder + "Data.sav";
if(File.Exists(_savePath) == true)
{
return(JsonConvert.DeserializeObject<SaveData>(File.ReadAllText(_savePath)));
}
else
{
Debug.LogWarning("SaveFile could not be found in: " + _savePath);
}
}
[System.Serializable]
public class SaveData
{
[SerializeField] public GlobalGameStatistics.ProgressData ProgressData = new GlobalGameStatistics.ProgressData();
[SerializeField] public GlobalGameStatistics.StatisticsData StatisticsData = new GlobalGameStatistics.StatisticsData();
public SaveData(SaveData data)
{
ProgressData = data.ProgressData;
StatisticsData = data.StatisticsData;
}
public SaveData(GlobalGameStatistics.ProgressData progress, GlobalGameStatistics.StatisticsData statistics)
{
ProgressData = progress;
StatisticsData = statistics;
}
}
The ScriptableObject I'm trying to save:
[CreateAssetMenu(fileName="MissionTemplate", menuName="Game/Mission Template"), Serializable]
public class MissionTemplate : ScriptableObject
{
[Header("Settings:")]
public float time;
public MapTemplate map;
public PopulationTemplate population;
public TargetTemplate target;
public StoryMessage[] messages;
#region Classes
[Serializable]
public class StoryMessage
{
[HorizontalGroup("Header")]
public string headline;
[HorizontalGroup("Header")]
public string sender;
[TextArea(8, 8)] public string text;
[HideInInspector] public bool read;
}
#endregion
}
The GlobalGameStatistics.Progress
class is where a list of MissionTemplate
s is contained which should be saved.
MapTemplate
,PopulationTemplate
, etc. that you have not provided. You should slim this down to a synthetic example that shows the kinds of serialization operations you need to support without the full dependency complexity of your whole game. \$\endgroup\$