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!