How do I get separate ScriptableObject variables from a list?

Hello! I am pretty new to Unity (downloaded it a few years ago but only just started using it) and I am working on an inventory system where each item is a ScriptableObject with GameObject, range, damage, health, etc. variables. I am trying to make a more versatile system where I can use a list to have from 1 to 20 items and have them switch out, but get the variables from the ScriptableObjects in the list separately from one another. Any help would be appreciated, and thank you to anyone who helps out in advance!
-ThatOnePurpleKid

Do you mean like you have 20 slots in inventory on your player or 20 items total in a game?

2 Answers

2

Let’s assume you have a list called myList that contains ScriptableObject variables of type MyScriptableObject. You want to access each individual ScriptableObject from the list and perform some operations on them.
using UnityEngine;
using System.Collections.Generic;

public class MyScriptableObject : ScriptableObject
{
// Your ScriptableObject variables and properties
// …
}

public class Example : MonoBehaviour
{
public List myList;

private void Start()
{
    // Iterate through the list and access each ScriptableObject
    foreach (MyScriptableObject obj in myList)
    {
        // Perform operations on each ScriptableObject
        // For example, you can access their properties or call their methods
        Debug.Log(obj.property1);
        Debug.Log(obj.property2);
        obj.Method1();
        // ...
    }
}

}
In this example, the Start() method iterates through the myList and accesses each individual MyScriptableObject. You can then perform any desired operations on each ScriptableObject, such as accessing their properties or calling their methods.

Note that you would need to populate the myList with the actual ScriptableObject instances before running this code.

Check this project out it is free, tough me a LOT!!

Picked it apart many times over the years and use it as my base layout for making custom editors. can be used pretty well out of box but I’d tinker and play with it. shows you both sides, the custom editor stuff and the in game inventory management.