Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Error when saving for the first time | Save/Load System

Discussion in 'Scripting' started by Nightland_Driver, Apr 12, 2022.

  1. Nightland_Driver

    Nightland_Driver

    Joined:
    Nov 21, 2021
    Posts:
    13
    Hello,

    I have a problem with a NullReferenceException which I can’t find.
    I have programmed a save/load system with the help of the following tutorial:

    Unfortunately, I have the problem that whenever I like to save for the first time the error occurs.
    The error is fixed by clicking the first time on Load.

    Can someone help me with this problem?

    The Error:

    NullReferenceException: Object reference not set to an instance of an object
    PlotManager.SaveData (GameData data) (at Assets/Scripts/PlotManager.cs:68)
    DataPersistenceManager.SaveGame () (at Assets/Scripts/SaveSystem/DataPersistenceManager.cs:50)
    PauseManager.SaveGameBtn () (at Assets/Scripts/PauseManager.cs:17)
    ...


    Plot Manager:
    Code (CSharp):
    1. public void SaveData(GameData data) {
    2.     if (data.isBought.ContainsKey(id)) { //Line 68
    3.         data.isBought.Remove(id);
    4.     }
    5.     data.isBought.Add(id, isBought);
    6. }
    Data Persistence Manager:
    Code (CSharp):
    1. public void SaveGame() {
    2.     foreach (IDataPersistence dataPersistenceObj in dataPersistenceObjects) {
    3.         dataPersistenceObj.SaveData(gameData); //Line 50
    4.     }
    5.  
    6.     dataHandler.Save(gameData);
    7. }
    PauseManager:
    Code (CSharp):
    1. public void SaveGameBtn() {
    2.     DataPersistenceManager.instance.SaveGame(); //Line 17
    3. }
     
  2. Terraya

    Terraya

    Joined:
    Mar 8, 2018
    Posts:
    646
    As your error says, it occurs on line 68 in your code.

    Either you can send here in your Code or check line 68.
     
  3. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,748
    Either "data" is null or "data.isBought" is null. I'm guessing isBought probably needs to be initialized.
     
  4. Nightland_Driver

    Nightland_Driver

    Joined:
    Nov 21, 2021
    Posts:
    13
    Thank you for your quick answer. That’s right. It’s not initialized.
     
  5. technofeeliak

    technofeeliak

    Joined:
    Oct 2, 2017
    Posts:
    22
    The guy who made the video made mistakes and offers corrections in this one.

     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,962
    Believe it or not, there's one answer that works for ALL NullReferenceExceptions.

    The answer is always the same... ALWAYS!

    How to fix a NullReferenceException error

    https://forum.unity.com/threads/how-to-fix-a-nullreferenceexception-error.1230297/

    Three steps to success:
    - Identify what is null <-- any other action taken before this step is WASTED TIME
    - Identify why it is null
    - Fix that