Hello!
First of all, sorry if my post isn’t perfect, this is my first time posting here for help.
Context: For a project I’m currently working on I decided to define UnitStats as scriptables objects (so it can contain icons, min/max values, etc…). I have a MovePointsDefinition (SO) which is used in the UnitDefinition (SO as well). UnitDefinition contains a Dictionary<StatDefinition, int> which defines the unit value for each stat (pretty self explanatory).
When spawning a Unit (not SO) I’m iterating through the stats referenced in the corresponding UnitDefinition, and store the “current” value of the stat in a Dictionary<StatDefinition, int> (so for example i would have key = MovePointsDefinition and value = 10).
In order to facilitate access to MovePoints through code (and possibly interpreted code later on) I decided to implement a MovePoints property. In order to access MovePoints value without a direct reference to the MovePointsDefinition SO, I decided to create a SingletonSO (yes I know) which contains references to the StatDefinitions I want to access easily (bascially a Database of hard referenced StatDefinition SO).
Therefore, Unit has a Property MovePoints which basically says this: int MovePoints => this.statContainer[StatAccessor.Instance.MovePointsDefinition];
This works perfectly in Editor, but fail dramatically in Build, as I’m getting a KeyNotFoundException: The given key 'MovePointsStatDefinition' was not present in the dictionary.
I understand that the InstanceID are indeed different in Build, but I struggle to understand exactly why (I guess Unity is loading my MovePointsDefinition twice, even though it is a single Asset), and most importantly: is there a fix ? I initially thought Addressables could fix this, but I’m not very experienced with it and even after tagging my StatDefinitions, StatAccessor and UnitDefinitions as Addressables, the comparison still doesn’t work.
I know I could fix this by using a unique serialized GUID and compare it, or other less attracting solutions (such as using enums, which I don’t want to). But I would like to understand perfectly why this is happening and is this just a bad usage of SO or is it a bug that might be fixed in the future.
Also, I simplified the code here as I believe this issue is based on Unity’s way of handling SO in Build vs in Editor, but know that there is a bunch of abstraction going on in this code, in case this could be the source of my problem.
I can give further explanations or code snippets if needed.
Thanks for the help!