Custom editor - scriptable object not saving data

     using System.Collections;
     using System.Collections.Generic;
     using UnityEngine;
     using UnityEditor;
     using System;
     using UnityEditor.SceneManagement;
    
     [System.Serializable]
     [CustomEditor(typeof(Wave))]
     public class WaveEditor : Editor {
    
    
    
    
         int numberOfSegments = 1;
    
         public override void OnInspectorGUI()
         {
             Wave myTarget = (Wave)target;
          
    
    
             EditorGUI.BeginChangeCheck();
             numberOfSegments = EditorGUILayout.IntField("Number of wave segments:", numberOfSegments);
             if (EditorGUI.EndChangeCheck())
             {
                 if (numberOfSegments < 1)
                     numberOfSegments = 1;
                 Array.Resize(ref myTarget.waveSegments, numberOfSegments);
                 Array.Resize(ref myTarget.waveSegmentsDelay, numberOfSegments - 1);
    
             }
    
            
    
    
    
             for (int i = 0; i < myTarget.waveSegments.Length; i++)
             {
                 myTarget.waveSegments[i]=(WaveSegment)EditorGUILayout.ObjectField(myTarget.waveSegments[i], typeof(WaveSegment), true);
                 if (i != myTarget.waveSegments.Length - 1) myTarget.waveSegmentsDelay[i] = EditorGUILayout.FloatField("Delay", myTarget.waveSegmentsDelay[i]);
             }
    
             if (GUI.changed)
             {
                 Undo.RecordObject(myTarget,"save");
                 EditorUtility.SetDirty(myTarget);
                 EditorSceneManager.MarkSceneDirty(EditorSceneManager.GetActiveScene());
                 AssetDatabase.SaveAssets();
                 Repaint();
                 Debug.Log("SAVED");
             }
    
    
          
              
         }
    
     }
    
     using System.Collections;
     using System.Collections.Generic;
     using UnityEngine;
     using UnityEditor;
     [System.Serializable]
     [CreateAssetMenu(fileName = "Wave", menuName = "Wave/Wave", order = 3)]
     public class Wave : ScriptableObject
     {
    
         public WaveSegment[] waveSegments;
         public float[] waveSegmentsDelay;
    
    
         private void Awake()
         {
             waveSegments = new WaveSegment[1];
          
         }
    
     }

i tried everything but still not saving…

Where are you creating instances of Wave, and writing the assets to file?

In custom editors you should try to only modify SerializedProperties, doing stuff directly on the target gets into the twilight zone. If you do SerializedProperties a bunch of stuff just works.

You need to set a scriptable object as dirty, not the scene. ScriptableObjects are never part of the scene actually.

Try using:

EditorUtility.SetDirty(YourScriptableObjectRef);

Instead of MarkSceneDirty.

Plus, SO’s are saved once the project saves. Ctrl->S / Save Project;
Plus, do serializedObject.Update() at the beggining of the OnInspectorGUI & serializedObject.ApplyModifiedProperties() at the end of the method otherwise inspector won’t update any value passed.

Also, note that in an actual “Wave” SO you’re wiping data in .Awake(). Don’t do that.

9 Likes

Thanks, this helped me. I don’t understand why these statements aren’t in the instructions on how to use SettingsProvider: https://docs.unity3d.com/2019.1/Documentation/ScriptReference/SettingsProvider.html

Without those two calls, none of the changes I made in the Project Settings screen would persist to the scriptable object.