r/Unity3D•
SwillyDo

Is there a convenient way to stop ScriptableObjects from suffering permanent changes during runtime?

Question

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

514
PointlessrebootProfessional - Engine Programmer•

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..

3
SwillyDoOP•

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.

-1
PointlessrebootProfessional - Engine Programmer•

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..

5
Loraash•

The game should never be able to write to ScriptableObjects..

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.

2
SilentSin26Animancer, InspectorGadgets, Weaver, UltEvents :redditgold:•

You need a save/load system to store anything permanently, and the core scriptable object remains a read-only asset.

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.

1
xyonico•

You can use Resources.UnloadAsset to throw out any modifications of the asset that are in memory and haven't yet been saved on disk. This is how I implemented it in one of my ScriptableObjects:

#if UNITY_EDITOR
using UnityEditor;
#endif

[CreateAssetMenu]
[InitializeOnLoad]
public class YourScriptableObject : ScriptableObject
{
	#if UNITY_EDITOR
	private void OnEnable()
	{
		EditorApplication.playModeStateChanged += OnPlayModeStateChanged;
	}

	private void OnPlayModeStateChanged(PlayModeStateChange state)
	{
		if (state != PlayModeStateChange.ExitingPlayMode) return;
		Resources.UnloadAsset(this);
	}
	#endif
}

The [InitializeOnLoad] attribute will make Unity invoke the OnEnable method when play mode starts.

Just be aware that this will also throw away any unsaved modifications you've made before hitting play.

3
SwillyDoOP•

That is awesome, thanks! When you say "unsaved modifications", I thought any changes I made to a ScriptableObject were saved automatically - does it get saved with the scene save command?

1
xyonico•

No, it gets saved with the Save Project command.

2
[deleted]•

i dont have a solution but i must echo your frustration with the disparity between editor and built behaviour. Ive had scriptable objects which instantiate other C# objects that behave absolutely fine during editor play, but then when building the scene the object absolutely tweaks out and spits out errors for no discernible reason, with the only apparent fix being to move instantiation to a monobehaviour.

2
Loraash•

You don't get a box to tick, but there's a way of writing them that makes this seamless.

For your public or [SerializeField] variables, only ever use them as default values. Copy them over to your runtime value at start, and only deal with the second value. Having a property for instance is really convenient, it's not only .NET best practice to have your public values be accesses through a property but Unity doesn't serialize public properties by default. Win-win.

1
SwillyDoOP•

I get that, but man, when you have a very detailed object with 20+ unique values, that's a really tedious way to do it.

1
Loraash•

Just add a snippet to VS to write it for you.

1
SwillyDoOP•

We're talking about creating runtime versions of the default variables, right? You have to type out all of the new variables - what value would a snippet be?

EDIT: Hmm, seems like you can generate properties...that sounds useful.

1
Loraash•

Your snippet would look something like

public <type> <name_lowercase>; // or [SerializeField] private
public <type> <name_uppercase> {get; set;}

And then set the uppercase name to contain the lower-case name on OnEnable or something. If you truly hate writing one line per variable in your startup method then you could write one universal method to set up an arbitrary ScriptableObject at startup using reflection, but this comes with all the drawbacks of using reflection.

Another method that's used by That One Talk is that you create classes for FloatValue, IntValue, etc. and use your SOs at that granularity. That way you only write the initialization code once per SO class and it's reused for all the values that you then create.

2