Does not find information to add to the list

I ran into a problem: I want to save the List using PlayerPrefs(JSON format) (acts as inventory for storing data about first aid kits). At the beginning of the game, the list is, of course, empty. And when charging an item through a third-party script by calling an event and me, it does not find either the object class, or the object itself (which, of course, is unlikely). But as soon as I add any S to the list, everything works. What’s wrong?

You cannot load / save UnityEngine.Object-derived stuff that way.

The reason is that you cannot call new to make new ones.

Load/Save steps:

An excellent discussion of loading/saving in Unity3D by Xarbrough:

Loading/Saving ScriptableObjects by a proxy identifier such as name: ← this might help

When loading, you can never re-create a MonoBehaviour or ScriptableObject instance directly from JSON. The reason is they are hybrid C# and native engine objects, and when the JSON package calls new to make one, it cannot make the native engine portion of the object.

Instead you must first create the MonoBehaviour using AddComponent() on a GameObject instance, or use ScriptableObject.CreateInstance() to make your SO, then use the appropriate JSON “populate object” call to fill in its public fields.

If you want to use PlayerPrefs to save your game, it’s always better to use a JSON-based wrapper such as this one I forked from a fellow named Brett M Johnson on github:

Do not use the binary formatter/serializer: it is insecure, it cannot be made secure, and it makes debugging very difficult, plus it actually will NOT prevent people from modifying your save data on their computers.

You were already told you can’t use scriptable objects for save data. Scriptable objects are for authoring data in the editor only.

Save data needs to be handled with non-Unity objects using basic data types. I would avoid player-prefs too. Just write it to disk.

Oh that’s right… your memory is better than mine.

Here’s what happened back then:

https://discussions.unity.com/t/930947/5

no, you misunderstood me. in the list I keep SO itself, without changing it at all and without touching it in any way. I have a SO type list that stores this item.

Also: show us your code please. :wink:

While researching the codes in search of a problem, I found that the script as a component does not have an activation check mark, and it seems to me that the problem is related to this. How to solve it? I rewrote the script, deleted and created a new one, put it in different GO, but nothing helped. I concluded that the problem is in the inventory code itself.

Changed: no, that’s not the problem

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class InventoryData : MonoBehaviour
{
    public List<HealData> healInventory;
    public List<WeaponData> weaponInventory;
    public List<OtherItemData> otherInventory;

    public const string saveKey = "InventoryData";

    void Awake()
    {
        Load();
    }

    public void HealAdded(HealData heal)
    {
        healInventory.Add(heal);
        Save();
    }

    public void WeaponAdded(WeaponData weapon)
    {
        weaponInventory.Add(weapon);
        Save();
    }

    public void OtherAdded(OtherItemData item)
    {
        otherInventory.Add(item);
        Save();
    }


    public void Load()
    {
        var data = SaveManager.Load<SaveData.InventoryData>(saveKey);
        healInventory = data.heals;
        weaponInventory = data.weapon;
        otherInventory = data.otherItems;
    }

    public void Save()
    {
        SaveManager.Save(saveKey, InventoryScriptSnapshot());
        Load();
    }

    private SaveData.InventoryData InventoryScriptSnapshot()
    {
        var data = new SaveData.InventoryData()
        {
            heals = healInventory,
            weapon = weaponInventory,
            otherItems = otherInventory,
        };
        return data;
    }
}

(Please don’t criticize how the code is written)

The problem is solved, it turns out PlayerPrefs could not create a JSON file for further use. Thanks!

Unity automatically adds or removes the checkbox depending on whether your component needs it or not. This is not an error or anything.