On Load Game, ArgumentNullException occurs

Hello there,
I am working on main menu and i’m getting a ArgumentNullException error when i click on continue game or load saved game.

FYP - Main Menu - PC, Mac & Linux Standalone - Unity 2020.3.20f1 Personal DX11 24_05_2022 4_46_41 pm

I think the problem is somewhere in the code given below:

private void SetCurrentSave(string saveFile)
        {
            PlayerPrefs.SetString(Key, saveFile);
        }

        private string GetCurrentSave()
        {
            return PlayerPrefs.GetString(Key);
        }

The savingWrapper.cs is given below:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using GameDevTV.Saving;
using RPG.Fade;
using UnityEngine.SceneManagement;

namespace RPG.SceneManagement
{
    public class Savingwrapper : MonoBehaviour
    {
         const string Key = "currentSaveName";
        [SerializeField] float fadeIntime = 0.2f;
        [SerializeField] float fadeouttime = 0.2f;
        [SerializeField] int firstFieldBuildIndex = 1;

        public void Continuegame()
        {
            StartCoroutine(LoadLastScene());
        }

        public void LoadGame(string saveFile)
        {
            SetCurrentSave(saveFile);
            Continuegame();
        }

        public void NewGame(string saveFile)
        {
            SetCurrentSave(saveFile);
            StartCoroutine(LoadFirstScene());
        }

        private void SetCurrentSave(string saveFile)
        {
            PlayerPrefs.SetString(Key, saveFile);
        }

        private string GetCurrentSave()
        {
            return PlayerPrefs.GetString(Key);
        }

        private IEnumerator LoadLastScene()
        {
            Fader f = FindObjectOfType<Fader>();
            yield return f.FadeOut(fadeouttime);
            yield return GetComponent<SavingSystem>().LoadLastScene(GetCurrentSave());
            //yield return GetComponent<SavingSystem>().LoadLastScene(DefaultSaveFile);
            yield return f.FadeIn(fadeIntime);

        }

        private IEnumerator LoadFirstScene()
        {
            Fader fader = FindObjectOfType<Fader>();
            yield return fader.FadeOut(fadeouttime);
            yield return SceneManager.LoadSceneAsync(firstFieldBuildIndex);
            yield return fader.FadeIn(fadeIntime);
       
        }

        


        const string DefaultSaveFile = "save";
        // Update is called once per frame
        void Update()
        {
            if (Input.GetKeyDown(KeyCode.L))
            {
                Load();

            }

            if (Input.GetKeyDown(KeyCode.S))
            {
                Save();

            }

            if (Input.GetKeyDown(KeyCode.Delete))
            {
                Delete(); 

            }

        }

        public void Save()
        {
            GetComponent<SavingSystem>().Save(GetCurrentSave());
            //GetComponent<SavingSystem>().Save(DefaultSaveFile);
        }

        public void Load()
        {
            GetComponent<SavingSystem>().Load(GetCurrentSave());
            //GetComponent<SavingSystem>().Load(DefaultSaveFile);
        }

        public void Delete()
        {
            GetComponent<SavingSystem>().Delete(GetCurrentSave());

I suspect your issue is here. If the key does not yet exist in PlayerPrefs, then it will return null, which is not what we want. Try adding a default value to the GetString

private string GetCurrentSave()
{
     return PlayerPrefs.GetString(Key, "defaultSave");
}

If the key does not exist in PlayerPrefs, then “defaultSafe” will be returned.

hi @Brian_Trotter
I got the save error and using defaultSave didn’t work.
please help, i’ve been stuck at this problem for a week now.

Can you post the details of the error? (select the error in the console, details will appear in the bottom of the console)

It looks like you might have an InventoryItem without a proper ItemID.
Open up InventoryItem, and navigate to the GetFromID() method.
In the foreach loop, before the if(itemLookupCache.ContainsKey(item.itemID)), add this line:

if(item.itemID==null)
{
    Debug.Log($"{item.name} does not have an itemID!");
    continue;
}

This should both prevent the error and give you an idea which InventoryItem is causing the issue.

2 Likes

Thanks I solved the error

Privacy & Terms