Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Error or bug?

Discussion in 'Scripting' started by Alacer, Jul 14, 2017.

  1. Alacer

    Alacer

    Joined:
    Jun 22, 2016
    Posts:
    14
    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+<ListenForSaveEventDelayed>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.

    Code (csharp):
    1.  
    2. saveAndLoad.SetPlayerValues (playerStatsToSave.money, playerStatsToSave.playerSkillFire, playerStatsToSave.playerSkillSword);
    3.  
    My save and load script

    Code (csharp):
    1.  
    2.  
    3. public savedataplayer data;
    4.  
    5. public List<PlayerSaveDataStats> playerSaveData = new List<PlayerSaveDataStats> ();
    6.  
    7.  
    8. public void SetPlayerValues(int _money, int _skillFire, int _skillSword)
    9. {
    10. playerSaveData [0].money = _money; //THIS is line 168
    11. playerSaveData [0].skillFire = _skillFire;
    12. playerSaveData [0].skillSword = _skillSword;
    13. }
    14.  
    15. [System.Serializable]
    16. public class savedataplayer
    17. {
    18. public List<PlayerSaveDataStats> playerSaveData2 = new List<PlayerSaveDataStats> ();
    19. }
    20.  
    21. [System.Serializable]
    22. public class PlayerSaveDataStats
    23. {
    24. public int money;
    25. public int skillFire;
    26. public int skillSword;
    27. }
    28.  
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,186
    Code (CSharp):
    1. public void SetPlayerValues(int _money, int _skillFire, int _skillSword)
    2. {
    3. playerSaveData [0].money = _money; //THIS is line 168
    4. playerSaveData [0].skillFire = _skillFire;
    5. playerSaveData [0].skillSword = _skillSword;
    6. }
    Your list is probably empty, thus index 0 has nothing

    do
    Code (CSharp):
    1. public void SetPlayerValues(int _money, int _skillFire, int _skillSword)
    2. {
    3. PlayerSaveDataStats.Add(new savedataplayer()
    4. {
    5.    money = _money, //THIS is line 168
    6.    skillFire = _skillFire,
    7.    skillSword = _skillSword
    8. });
    9. }
     
  3. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    first glance I think the issue is that playerSaveData is empty
    please do a Debug.Log(playerSaveData.Count);

    make sure it's not null
     
  4. Alacer

    Alacer

    Joined:
    Jun 22, 2016
    Posts:
    14
    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?
     
  5. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,773
    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.
     
    cstooch likes this.
  6. Alacer

    Alacer

    Joined:
    Jun 22, 2016
    Posts:
    14
    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.
     
  7. cstooch

    cstooch

    Joined:
    Apr 16, 2014
    Posts:
    354
    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...
     
    Last edited: Jul 14, 2017
  8. Alacer

    Alacer

    Joined:
    Jun 22, 2016
    Posts:
    14
    But i do, on start i got
    Code (CSharp):
    1. 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.
     
  9. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,186
    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...
     
    Last edited: Jul 14, 2017
    cstooch likes this.
  10. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,773
    Why are you using an array or list at all? Why not just a single variable?
     
    Brathnann and cstooch like this.
  11. Alacer

    Alacer

    Joined:
    Jun 22, 2016
    Posts:
    14
    Ahh i get it now. And thanks for the Add to my list, i really wasnt quite sure how to do that :)
    Awsome!
     
  12. Alacer

    Alacer

    Joined:
    Jun 22, 2016
    Posts:
    14
    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.