Scriptable object's variable serialization issue

I have a dictionary stored in a ScriptableObject, which then is saved as an asset.

Dictionary<Transform,Nodescript> scriptViaTransform= new Dictionary<Transform,Nodescript>();

Once I load the asset, my editor classes can pass values for storage, but if I Enter/Exit playmode, it looks like the dictionary re-initializes itself. This wipes all the keys/values :<

Question, how to store the dictionary data in a Scriptable object so that its dictionary doesn’t re-initialize itself upon the initialization? And how do I initialize dictionary only once?

Thank you

Unity cannot serialize dictionaries by default, so it ends up wiping it on recreating the instance. Have a look at Unity - Scripting API: ISerializationCallbackReceiver.OnBeforeSerialize to implement dictionary serialization.

Thank you, it’s exactly what I was looking for.

I have a very strange question in addition, but if I would be referencing an object with a “new” keyword in the scriptable object, granted that it’s not a dictionary, does it mean the variable gets initialized with a new object every time this Scriptable object asset is loaded into other classes?