- Home /
ScriptableObject asset not saved to disk
I have this ScriptableObject class:
public class ExampleScriptableObject : ScriptableObject {
public string testName;
public int value;
}
which I instantiate with this code:
[MenuItem("Assets/Test/ExampleScriptableObject")]
public static void CreateExampleScriptableObject() {
ScriptableObjectUtility.CreateAsset<ExampleScriptableObject>();
}
and the access it from this MonoBehaviour:
class ExampleMonoBehaviour : MonoBehaviour {
public ExampleScriptableObject ExampleScriptableObject;
public string TestName;
public int Value;
private void OnValidate() {
ExampleScriptableObject.testName = TestName;
ExampleScriptableObject.value = Value;
}
}
Each time I change value in the inspector and save scene/project, the change is only reflected in the scene file;
but asset file remains empty:
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!114 &11400000
MonoBehaviour:
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 1929e198d1f69204cbed8c2482e19fe9, type: 3}
m_Name: Test.ExampleScriptableObject
m_EditorClassIdentifier:
How can I save changes made to asset to disk instead of to the scene file?
Answer by metroidsnes · Mar 19, 2015 at 07:51 AM
Create custom inspector for the MonoBehaviour and in OnInspectorGUI() call EditorUtility.SetDirty() with the ScriptableObject asset passed as argument.
Answer by chakalaka · Mar 19, 2015 at 08:43 AM
call
EditorUtility.SetDirty(ExampleScriptableObject);
to save your object
Answer by DSL-TailorIt · Jun 12, 2017 at 06:40 AM
Since this is the top Google result for this issue, I'd like to complement the previous answers by suggesting also taking a look at AssetDatabase.SaveAssets() which effectively saves the changes to disk.
Useful for automated tools and stuff.
In my situation (a scriptable object is modified by an editor script) it didn't work all the time. I used SetDirty(myObject)
ins$$anonymous$$d and it helped (the asset got updated in the editor and by saving the scene it was saved to disk) — but only after I commented out the line with AssetDatabase.SaveAssets()
! So basically my problem was solved, but I would still like to see a solution which would automatically save changes to a ScriptableObject to disk whenever I want.
You're a legend. This worked beautifully for me. All I was trying to do was set a single string password in my scriptable object but it was not saving to file, calling AssetDatabase.SaveAssets() fixed my issue.
Thank you!!
Your answer
Follow this Question
Related Questions
Saving a ScriptableObject .asset on Android? 1 Answer
Why unity can't create/save asset (ScriptableObject ) at runtime ? 1 Answer
Export Settings or custom files to release builds 1 Answer
Prefab as a subasset of a ScriptableObject 0 Answers
Why does my List overwrite itself when using .Add? 2 Answers