Is there a convenient way to stop ScriptableObjects from suffering permanent changes during runtime?
Outside of the editor the functionality is for all changes made to scriptable objects to be lost between loads, so it is bizarre that changes during runtime in the editor make permanent changes, and it makes development and testing very annoying. There are a few workarounds, like using proxy values that get reset, but when you have 20+ variables that could be changed it is very tedious to manually create and reset proxies for all of them.
You often have to jump through hoops to make editor changes to data permanent, it seems bizarre that the editor-runtime would explicitly have different functionality to normal runtime.
Honestly I would just love if there was a box to tick or an attribute to add to the class that renders runtime changes temporary.
I have tried instantiating an instance of the scriptableobject and working on that, but I got bizarrely inconsistent behaviour - Unity seemed to still be acting on the original sometimes rather than the instantiated copy. I might try again, as I have seen this solution mentioned in several places.
Posted Oct 1, 2019, 1:28 AM UTC · edited
ScriptableObjects are just like any other asset in unity they are modifiable in the editor and non savable at runtime..
And just like some assets at runtime the changes are kept until unloaded..
You should be treating it this way and not designing solutions that break this behaviour..
If you put data in you ScriptableObjects that you modify, then of course it's your responsibility to have it reset at the appropriate time..
I get that, but it is frustrating from a purely pragmatic point of view. For example, you can store a character's stats as a scriptable object, and during play changes like, say, lost hitpoints, can be handled directly by the scriptable object. You need a save/load system to store anything permanently, and the core scriptable object remains a read-only asset. That's a perfectly viable system for the final product, but you can't test that system in Unity without tedious work-arounds. It would be nice if there was, as I said, an Attribute that could mark a ScriptableObject so that runtime changes aren't saved.
I would say your designing you ScriptableObjects wrong, they should be designed as opaque data, meaning read only. The game should never be able to write to ScriptableObjects..
This is what I do, I can configure them in the editor, but either playmode or runtime doesn't have write access..
Because if they did, how would you share these data types, since any script with a reference can modify it.. I consider this a bad design and practice to allow..
Writing transient data to ScriptableObjects is a popular and pretty nice way of abusing them to get better singletons with editor support. I'd say at this point it's an accepted usage of SOs.
There's your solution. You do need a proper save/load system and you should treat scriptable objects as read-only assets, not try to modify them and shoehorn in some functionality to reset them when a scene gets unloaded. Nothing is stopping you from doing exactly that in the editor the same as at runtime.