hello :)
I wish to add each and every instante of an scriptable object class to a HashSet (which is in a static class in a separate script).
I did this by creating a method that adds said class to the hashset and I called this method in the constructor of the class I'm instantiating. Here's the thing though... it works, but not always? Here's the code, I'll go further into the problem at the end:
//first script where the hashset is:
public static class Record
{
private static HashSet<MyClassToInstantiate> MyHashSet = new HashSet<MyClassToInstantiate>();
public static void AddToMyHashSet(MyClassToInstantiate obj)
{
MyHashSet.Add(obj);
}
}
//the other script:
[CreateAssetMenu(menuName = "X")]
public class MyClassToInstantiate : ScriptableObject
{
[SerializeField] string someRandomData1;
[SerializeField] int someRandomData2;
public MyClassToInstantiate()
{
Record.AddToMyHashSet(this);
}
}
This works perfectly except the first time I open Unity. The first time I always have to go into any instance of my "MyClassToInstantiate" and change something (anything) and it just starts working (I'm using the [createAssetMenu] attribute btw).
ps. I'm sure I have blatant theoretical problem that I'm not seeing! Thanks in advance to anyone who gives me some idea of what is going on