Home > Back-end >  Object reference not set to an instance of an object regarding Data Persistence, save/load methods
Object reference not set to an instance of an object regarding Data Persistence, save/load methods

Time:11-04

Apologies in advance for incorrect terminology. I've borrowed script that saves/loads data, which I can use by referencing an interface that has a save and load method. Say I have script1, with a variable called playerHealth, I can then inherit the interface, call the save and load methods, and in those methods basically write that playerHealth, when saved, replaces the playerHealth variable in the Gamedata script (which houses initial values, or 0 in this case). and when loaded, the playerHealth value in the save file replaces the playerHealth in script1.

Problem is, clicking the save button, which has attached to it a function to find all variables within all save/load method instances in the various scripts, script1 (playerHealth), script2 (enemyHealth), etc., and execute the save sequence, which replaces all those variables in the gamedata script to the updated values, gives me an error "NullReferenceException: Object reference not set to an instance of an object"

Looking at the specifics of the error, it points me to the save method called in script1, where there's only this single line of code

public void SaveData ( GameData data )
{
    data.playerHealth = playerHealth;
}

playerHealth in this case would be a public int.

To me, this code seems to make sense? When I click the save button, it should do everything mentioned above, meaning it will find this section in script1, reference current playerHealth in script1, and make the playerHealth in the save file equal to the current playerHealth. But the error pops up regardless.

[What did I do to try and Fix the Issue]

Well, not much, as I'm kind of stuck on what to do. I tried adding a debug.log before the single line of code to see if playerHealth actually has a value attached at that point, and it outputs the correct value. So playerHealth, at the point in code just before replacing the savefile value, does indeed have a current value. Help is much appreciated.

If I need to provide more information, please let me know and I will provide them ASAP.

EDIT

It is most likely data.playerHealth throwing the error from my subsequent attempts. But then it really doesn't make much sense to me. Here's the gamedata.cs file that the above line of code would reference:

[System.Serializable]
public class GameData
{
    public int playerHealth;

    public GameData()
    {
        this.playerHealth = 0;
    }
}

As you can see, I've set a value. So no reason why it should throw an object reference error, right?

CodePudding user response:

Without knowing how SaveData is called there is no way for people to know what is going on.


In any case, if the error points to data.playerHealth = playerHealth; in SaveData(...), then most likely data is null.

If data == null then data.playerHealth doesn't exist as data doesn't point to any memory location. If it isn't equal to null then the error will not point to this location.

Therefor you have to check where this function is called from and examine that piece of code. And whatever is passed on to Savedata.

For sure it isn't this.playerHealth = 0;.

CodePudding user response:

Sorry folks, I made a really simple mistake of not watching the full tutorial video, and understanding exactly what the code interactions are. I did not enable DontDestroyOnLoad(this.gameObject) on the gameobject that stores the necessary script for the save/load function. This resulted in there being no reference to get from in the following scenes, as the gameobject was destroyed. So yes, it was because Gamedata data was null. Lesson to self: don't be a codemonkey and blindly copy code down.

  • Related