How to I ensure my scriptable object constructor gets called?
I have a scriptable object that uses some readonly static structs. Because these structs are readonly and static their values can only be set in a constructor or upon initializing the variable, which is why I cannot simply put them in Awake or Start.
The constructor is parameter-less and simply uses the values from the scriptable object that have been set in the editor, and I am instantiating the scriptable object SpellObject like so:
private SpellObject Fireball;
Fireball = Instantiate(Resources.Load<SpellObject>($"Spells/Fireball"));
The problem is that although this clones the Fireball SpellObject successfully the constructor is never called.
A workaround would be to remove the "readonly" keyword when declaring the structs inside the SpellObject (i.e. private readonly Damage damage;
would become private Damage damage
) which allows them to be set inside Awake/Start, but this is not what I am intending to do with my code: the values set in the editor and used to create the structs should indeed remain readonly and never be overwritten during runtime (they are the base values for the spell and are needed throughout the game).
How can I ensure that the constructor is called when instantiating/cloning the scriptable object?
Thanks!
Your answer
![](https://koobas.hobune.stream/wayback/20220613020110im_/https://answers.unity.com/themes/thub/images/avi.jpg)