Adding values to a list from a ScriptableObject.

My ScriptableObject:

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

[CreateAssetMenu (fileName = "Hero", menuName = "Create HeroValuesAsset")]
public class HeroValues : ScriptableObject
{
    ListsScript listsScript = new ListsScript();
 
    [SerializeField]
    private int heroHP;
    [SerializeField]
    private string heroName;

    public int HeroHP
    {
        get
        {
            return heroHP;
        }
    }

    public string HeroName
    {
        get
        {
            return heroName;
        }
    }

   public void AddToList()
   {
        listsScript.generalList.Add(1);
   }
}

My ListsScript

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

public class ListsScript
{
    public List<int> generalList = new List<int>();
}

I have made 5 UI Buttons. Each of these buttons has a respective HeroValueAsset “attached” to it through OnClick() in the Inspector (the respective Scriptable Object int the Object section of the OnClick() and AddToList() selected as a desired method to be called). Each Scriptable Object contains two data values: int heroHP and string heroName. I’ve tested if it works, by passing the value to a method from the AddToList() method to the ListsScript, Debug.logging the passed value and it works. Great!

However i’m having a whole different issue now. When i try to add the value from HeroValues to the generalList in the ListsScript, the targeted list doesn’t get any values and stays at a count of 0 when i click the buttons (checked by Debug.Log from the ListsScript). I also tried checking by calling Debug.Log(listsScript.generalList.Count); from AddToList(). That adds the values to a some sort of a list, when i click the buttons, as each button adds values to their “own” list (i presume). For example: i click 3 times the a first hero button and it Debug.Logs 3 new counts, but clicking 5 times on the second button causes the count to “reset” and count from 0 to 5. Very odd. I assume it might be caused by the ListsScript listsScript = new ListsScript(); line, but i really don’t know what else i could do there.

My issue is: my generalList in the ListsScript doesn’t seem to recieve the ints that i’m trying to add to it. They seem to be added somewhere else and with a separate list for each ScriptableObject. I suspect that the initialization of the ListsScript might be the culprit, but i don’t know what else could i do to .Add the values the way i want to add them.

Any help is appreciated, thank you for your answers!

Every instance of your HeroValues scriptable object has its own local list. Those are not the same list.

Perhaps you want something like a PartyManager that receives button presses and adds new party members to a single master list int he Party Manager?

Greetings Kurt and thank you for a quick response! Yes, that is closer to what i’m looking for. I tried something similar when testing if the buttons output the correct values to the method and i think i’ll try going that way.

Just to be sure, all i need to to is to simply recieve the input from the buttons that i have and add it to the List using a method that is going to recieve the inputs, correct?