Error or bug?

I have never ever asked for any code help, because i always managed to solve it somehow. But this one eludes me and i must know…

I think i supplied most of the relevant code below, but here is my problem in specific:

When i hit my save button to fire off save event, i get this error.

NullReferenceException: Object reference not set to an instance of an object
SaveAndLoadPlayer.SetPlayerValues (Int32 _money, Int32 _skillFire, Int32 _skillSword) (at Assets/My Scripts/SaveAndLoad/SaveAndLoadPlayer.cs:168)
OnPlayer+c__Iterator0.MoveNext () (at Assets/My Scripts/SaveAndLoad/OnPlayer.cs:61)
UnityEngine.SetupCoroutine.InvokeMoveNext (IEnumerator enumerator, IntPtr returnValueAddress) (at C:/buildslave/unity/build/Runtime/Export/Coroutines.cs:17)

If i in the inspector click my player to select him, and then press my save button, all works fine with no errors. When i run the game, the default selected item in the inspector is DontDestroyOnLoad scene. and under that is my player. Has anyone encountered something similar?

Some more details below…

I got my player with dontdestroyonload, and a saveLoad system im working with. I also got a dummy script with some values to fetch and save.

My dummy script has this on an event.

saveAndLoad.SetPlayerValues (playerStatsToSave.money, playerStatsToSave.playerSkillFire, playerStatsToSave.playerSkillSword);

My save and load script

public savedataplayer data;

public List<PlayerSaveDataStats> playerSaveData = new List<PlayerSaveDataStats> ();


public void SetPlayerValues(int _money, int _skillFire, int _skillSword)
{
playerSaveData [0].money = _money; //THIS is line 168
playerSaveData [0].skillFire = _skillFire;
playerSaveData [0].skillSword = _skillSword;
}

[System.Serializable]
public class savedataplayer
{
public List<PlayerSaveDataStats> playerSaveData2 = new List<PlayerSaveDataStats> ();
}

[System.Serializable]
public class PlayerSaveDataStats
{
public int money;
public int skillFire;
public int skillSword;
}
public void SetPlayerValues(int _money, int _skillFire, int _skillSword)
{
playerSaveData [0].money = _money; //THIS is line 168
playerSaveData [0].skillFire = _skillFire;
playerSaveData [0].skillSword = _skillSword;
}

Your list is probably empty, thus index 0 has nothing

do

public void SetPlayerValues(int _money, int _skillFire, int _skillSword)
{
PlayerSaveDataStats.Add(new savedataplayer()
{
   money = _money, //THIS is line 168
   skillFire = _skillFire,
   skillSword = _skillSword
});
}

first glance I think the issue is that playerSaveData is empty
please do a Debug.Log(playerSaveData.Count);

make sure it’s not null

Sorry, i just posted the most relevant code.
The code is already inside a check for not null.
But like i said, when i click the player in the unity Hierarchy it all works fine. That is very odd.
I been working on this on and off for about a month now.
I recently updated to the latest unity version, could it be that?

if playerSaveData is a public variable, it’s not odd at all.

What’s happening is, you’re not initializing playerSaveData anywhere. So, by default, it is null. However, when you select the player and the inspector shows, the inspector creates an object - it won’t allow a member variable to be null (except for references to UnityObjects).

So, yeah, playerSaveData is null.

1 Like

I cant make it private.
I tried to check if its null on Start(), and it isnt.
Anyway, being a noob, what should i do? if i add something to playerSaveData, then it is initialized?
What should i do?

Oh and, thanks for your replies all of you, i really appreciate it.

Setting the access modifier to private wouldn’t fix your problem anyways (and that’s not what StarManta or anyone is suggesting). The problem is that you’re trying to access an index of a list when the list has nothing in it. You’re trying to jam some data in slot 0 (the first slot) of your list, but your list has no slots in it yet (no index of 0 yet… nada).

Use the Add method on your playerSaveData to add data. Actually, I even see @Brathnann already told you this above.

Let’s put it another way maybe easier to understand. The Add method would append data on to the end of your list. When you supply an index like you did (playerSaveData [0].money = _money; ), you’re actually saying to overwrite the value that’s already existing at this index. And again, the list (edit: sorry, I said array initially… meant list) is completely empty… no index 0, no index 1… nothing…

But i do, on start i got playerSaveData.Add (null); just to make the lenght to 1.
And its not an array, its a list. (I SHOULD be using an array since the lenght of it is always 1, i just dont know how to convert it).
But that doesnt matter, i dont get an index out of bounds error, i am almost sure my list isnt empty since i check for more it more than once, and on start up.

You do realize you just posted your own answer. You added a null entry…Then you’re trying to access null and set a value on it…but…it’s null…there is nothing there…

So, either you add the values like I showed you, or you add an object to it like you’re doing, but just have the ints be 0 if you need. Since this is saved data, you’re probably loading it up again anyways. Just don’t add null…(or delete it, but really, no reason to add it)

Chances are what you want is to try to load save data, if you find some, assign it to your list, if you don’t, you just add a new entry before you save it out. (so you could just check the count >0 before you save and if it’s not, then add new entry. if it is, overwrite the first index)

As for an array that is public, you just set the array count to 1 in the inspector and that will handle it for you…

Edit: And to add, yes, as I was thinking about it, the error didn’t match up and should have been an out of index range, but now it makes sense…

1 Like

Why are you using an array or list at all? Why not just a single variable?

2 Likes

Ahh i get it now. And thanks for the Add to my list, i really wasnt quite sure how to do that :slight_smile:
Awsome!

Well, im not that skilled in coding. I think i am good at altering others code to my own needs, and also learning that way.
Anyway, the guy that made the saveAndLoad had a list in it. I needed to save some player values and also a list of items.
Single variable for all the skills and money, hmm… i dont know, a list just seemed like the way to go, so i could always just add on to it if i needed to add something more to save.

I think i got an enum list now that i think about it, and i convert it to a list, and then plan to save it? I dont remember, i’ve just been working on this test scene to get save and load to work before putting it into my game.