How to save and load list of scriptable object ?

I have a inventory . The items of the inventory are scriptable object

[CreateAssetMenu(menuName = "Item", fileName = "New Item")]
public class Item : ScriptableObject
{
    public string itemName;
    public int itemPrice;
    public Sprite itemSprite;
}

This is the inventory script

public class GameManager : MonoBehaviour
{
    public static GameManager instance;

    public List<Item> items = new List<Item>();
    public List<int> itemNumbers = new List<int>();

    public GameObject[] slots;

    public ItemButton thisButton;
    public ItemButton[] itemButtons;
    
    private void Awake()
    {
        if (instance == null)
        {
            instance = this;
        }
    }

    private void Start()
    {
        DisplayItems();
    }

    private void DisplayItems()
    {
        for (int i = 0; i < slots.Length; i++)
        {
            if (i < items.Count)
            {
                slots*.gameObject.SetActive(true);*

slots_.transform.GetChild(0).GetComponent().sprite = items*.itemSprite;_
slots.transform.GetChild(1).GetComponent<TMP_Text>().text = itemNumbers.ToString();
_}
else
{_

_slots.transform.GetChild(0).GetComponent().sprite = null;_
slots.transform.GetChild(1).GetComponent<TMP_Text>().text = null;*

slots*.gameObject.SetActive(false);*
}
}
}

public void AddItem(Item _item)
{
if (!items.Contains(_item))
{
items.Add(_item);
itemNumbers.Add(1);
}
else
{
for (int i = 0; i < items.Count; i++)
{
if (item == items*)*
{
itemNumbers*++;*
}
}
}
DisplayItems();
}_

public void RemoveItem(Item _item)
{
if (items.Contains(_item))
{
for (int i = 0; i < items.Count; i++)
{
if (item == items*)*
{
items.Remove(_item);
itemNumbers.Remove(itemNumbers*);*
}
}
}
ResetButtonItems();
DisplayItems();
}_

public void ResetButtonItems()
{
for (int i = 0; i < itemButtons.Length; i++)
{
if (i < items.Count)
{
itemButtons_.thisItem = items*;
}
else
{_

_itemButtons.thisItem = null;
}
}
}
}*_

How to save and load the inventory ?

Since your items are ScriptableObjects, they are also Assets. They exist as .asset files in your Folder hierarchy.

The easiets way for you to save the inventory is therefore to store a reference to the .asset file of your ScriptableObject. You would do this by having a list/array with the file reference of each of your inventory items. You can then serialize that list however you want and at deserialization load the ScriptableObjects from Resources or ASsetBundles or whatever using the file reference.

I am at work right now so this is some very basic pseudo-code for you:

//Item is your ScriptableObject class
//Ideally you will have a converter class that stores the resource location for each asset 
//in this example i will retrieve the resource location/path with a made-up static class "Locator"
//for example implementation of a resource path class, I will link a UF thread
void ConvertInventoryToFileReferences(List<Item> inventory) 
{
	List<string> resources = new List<string>();
	foreach(Item itm in inventory)
	{
		resources.Add(Locator.GetPathOf(itm));
	}
	string[] resourceReferences = resources.ToArray();
	//now you can use any serialization method you'd like, e.g. binaryformatter/ jsonUtility/ odin serializer to save this to a file
}

//You will pass this the array/list deserialized from binaryformatter/simplejson or whatever
List<Item> ConvertFileReferencesToItemInventory(List<string> invIn)//or string[] I will use list
{
	List<Item> invOut = new List<Item>(inventory.Count);
	foreach(string path in invIn)
	{
		//load your asset from for example resources
		//note: for resouces.load file extensions must be omitted, so make sure your locator class excludes the extension
		invOut.Add(Resources.Load<Item>(path));		
	}
	return invOut;
}

Additional information:

Example of a resource locator: 1

There is tons of resources available explaining how to serialize data into files, so here’s a few links on how to do it with various tools:

Serializing with jsonUtility

Serializing with binaryFormatter

I advise everyone to look at Odin Serializer, it is free and tailored to unity development and allows for serialization of virtually everything.

If from these examples you can’t figure out how to serialize and deserialize stuff, just check out these youtube videos:

Link1, Link2, Link3, Link4,.

I have given you the basis of this answer in another thread on here a few days ago, btw.