Load specific ScriptableObject in runtime by its ID

Hi guys, i’m trying to save weapons gameobjects into a file. One way I was thinking, is by serializing (save) only the ID of the weapon created inside its ScriptableObject, and then deserialize it (load) that ScriptableObject in runtime. The problem is that I don’t know how to load ScriptableObjects from the assets. How can I get a ScriptableObject with a specific ID, and then load it by searching through all the ScriptableObjects in a folder? I was trying with the Resources folder, but it’s not recommended by unity and it wouldn’t work because I can’t access the ID variable inside the ScriptableObjects.

This is the script of the ScriptableObject:

[CreateAssetMenu(menuName = "ScriptableObjects/Weapon")]
public class WeaponDataSO : ScriptableObject
{
    public string ID;
    public GameObject WeaponPrefab;
    public string Name;
    public float Damage;
    public float FireRateRPM;
    public int MaxBulletsMagazine;
    public bool SemiAuto;
    public bool FullAuto;
    public AudioClip ShootSound;

    public enum WeaponTypes
    {
        Rifle,
        SMG,
        Pistol
    }

    public WeaponTypes Type = WeaponTypes.Rifle;
}

You’ve got the right idea to serialise out just the ID and use that to look them up.

Basically you want to make a rudimentary database for your items. This can be as simple as another scriptable object that holds a reference all SO’s of a particular type. You save out the ID’s, and then when loading, you look up the ID’s in the database to restore the data.

Lots of ways this can be accomplished.

Maybe use addressables?

I found out about addressables when I was looking for this problem. I’ve never used them and I have to study them before. In particular I have to see how can I use them with ScriptableObjects

Addressables won’t help in this situation. It’s designed for streaming content in and out of memory. You still need the SO in memory to see what it’s ID is.