I’m attempting to implement a save and load system in my project but I keep on running into the following error whenever I press the save button:
NullReferenceException: Object reference not set to an instance of an object
System.Collections.Generic.Dictionary2[TKey,TValue].get_Keys () (at <9aad1b3a47484d63ba2b3985692d80e9>:0) System.Reflection.RuntimePropertyInfo.GetterAdapterFrame[T,R] (System.Reflection.RuntimePropertyInfo+Getter
2[T,R] getter, System.Object obj) (at <9aad1b3a47484d63ba2b3985692d80e9>:0)
System.Reflection.RuntimePropertyInfo.GetValue (System.Object obj, System.Object[ ] index) (at <9aad1b3a47484d63ba2b3985692d80e9>:0)
Rethrow as TargetInvocationException: Exception has been thrown by the target of an invocation.
System.Reflection.RuntimePropertyInfo.GetValue (System.Object obj, System.Object[ ] index) (at <9aad1b3a47484d63ba2b3985692d80e9>:0)
SaveLoadUtility.WriteToDictionary (System.Type fieldType, System.String fieldName, System.Object fieldValue, System.Collections.Generic.Dictionary2[System.String,System.Object]& baseDict, System.Boolean isField) (at Assets/Dead Earth/Scripts/Unity Save Load Utility/SaveLoadUtility.cs:924) SaveLoadUtility.GetValues (System.Object baseInstance, System.Collections.Generic.Dictionary
2[System.String,System.Object]& baseDict, System.Boolean checkForPropertySelector) (at Assets/Dead Earth/Scripts/Unity Save Load Utility/SaveLoadUtility.cs:676)
SaveLoadUtility.GetValues (System.Object baseInstance, System.Collections.Generic.Dictionary2[System.String,System.Object]& baseDict) (at Assets/Dead Earth/Scripts/Unity Save Load Utility/SaveLoadUtility.cs:570) SaveLoadUtility.WriteToDictionary (System.Type fieldType, System.String fieldName, System.Object fieldValue, System.Collections.Generic.Dictionary
2[System.String,System.Object]& baseDict, System.Boolean isField) (at Assets/Dead Earth/Scripts/Unity Save Load Utility/SaveLoadUtility.cs:966)
SaveLoadUtility.GetValues (System.Object baseInstance, System.Collections.Generic.Dictionary`2[System.String,System.Object]& baseDict, System.Boolean checkForPropertySelector) (at Assets/Dead Earth/Scripts/Unity Save Load Utility/SaveLoadUtility.cs:676)
SaveLoadUtility.PackComponent (System.Object component) (at Assets/Dead Earth/Scripts/Unity Save Load Utility/SaveLoadUtility.cs:564)
SaveLoadUtility.PackGameObject (UnityEngine.GameObject go, ObjectIdentifier oi) (at Assets/Dead Earth/Scripts/Unity Save Load Utility/SaveLoadUtility.cs:522)
SaveLoadUtility.SaveGame (System.String saveGameName) (at Assets/Dead Earth/Scripts/Unity Save Load Utility/SaveLoadUtility.cs:207)
SaveLoadMenu.OnGUI () (at Assets/Dead Earth/Scripts/Unity Save Load Utility/SaveLoadMenu.cs:197)
When I double click on the error in the editor I’m redirected to the following lines of code in the “SaveLoadUtility.cs” script:
//Go through the OIsToSerialize array again to pack the GameObjects into serializable form, and add the packed data to the sceneObjects list of the new SaveGame instance.
if(true) {
foreach (ObjectIdentifier oi in OIsToSerialize) {
//Convert the GameObject's data into a form that can be serialized (an instance of SceneObject),
//and add it to the SaveGame instance's list of SceneObjects.
newSaveGame.sceneObjects.Add(PackGameObject(oi.gameObject, oi));
}
}
I’m using this asset I discovered called “USLU” (linked below) which is meant to be straightforward yet I still can’t wrap my head around this error.
I’m going to attach some files that relate to this error in particular. There are a few requirements for the save system to work (which I’ve fulfilled), first is that the “ObjectIdentifier.cs” script must be attached to the prefabs that I wish to save and second is that those prefabs must be placed in the “Assets/Resouces/prefabs” folder created after importing the asset.
Lastly, I have written “GameSaveManager.cs” script which fetches the player’s data, like health and inventory, and encrypts/decrypts it on save/load. I will also attach this file, tho I don’t think this script is causing any issues.
9025639–1245295–ObjectIdentifier.cs (2.91 KB)
9025639–1245298–SaveLoad.cs (7.46 KB)
9025639–1245301–SaveLoadMenu.cs (5.82 KB)
9025639–1245304–SaveLoadUtility.cs (56.9 KB)
9025639–1245307–GameSaveManager.cs (16 KB)