Values in CustomEditor change when running scene

I have created a datepicker component. My aim is for it to have editable values in the inspector that stay the same after I run the “run” button in Unity3d. Just like how every other native component behaves (e.g. the Rect Transform: I change the position to (0, 0, 0) and when I run the GameObject in the scene stays right there).
At the same time I want a custom layout of the inspector so I use a CustomEditor.
This is the datepicker script (the part that is relevant):

public class DatePickerBehaviour : MonoBehaviour {

    public int SelectedYear {
        get {
            return selectedYear;
        }
        set {
            selectedYear = value;
        }
    }

    private int selectedMonth;

    public int SelectedMonth {
        get {
            return selectedMonth;
        }
        set {
            selectedMonth = value;
        }
    }

    private int selectedDay;

    public int SelectedDay {
        get {
            return selectedDay;
        }
        set {
            selectedDay = value;
        }
    }

.......
}

This is the CustomEdtior script:

[CustomEditor(typeof (DatePickerBehaviour))]
public class DatePickerEditor : Editor {

    string[] monthNames = new string[]{"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
    int[] monthNumbers = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
    int[] dayNumbers = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31};
    string[] dayNames = new string[]{"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31"};

    public override void OnInspectorGUI() {
        DatePickerBehaviour datePickerScript = (DatePickerBehaviour)target;

        datePickerScript.SelectedDay = EditorGUILayout.IntPopup("Selected day", datePickerScript.SelectedDay, dayNames, dayNumbers);
        datePickerScript.SelectedMonth = EditorGUILayout.IntPopup("Selected month", datePickerScript.SelectedMonth, monthNames, monthNumbers);
        datePickerScript.SelectedYear = EditorGUILayout.IntField("Selected year", datePickerScript.SelectedYear);
    }

}

The problem is: when I make a change in the inspector and hit run every value gets overwritten. Any ideas what could be the problem?
I also tried to use serialzedObject with ApplyModifiedProperties() but this changed nothing…

I think you should do 2 things:

  1. Unity will not serialize (save) any changes you make to those private fields. You should mark them with [SerializeField] attribute

  2. Once you’re done updating your object (after line 15 in the code you attached), you should tell Unity that the object is now “dirty” and should be saved. This is done using this call:

EditorUtility.SetDirty(target);

See the documentation for this method here: Unity - Scripting API: EditorUtility.SetDirty

Let me know if these suggestions helped :slight_smile:

Amazing.
It instantly worked! And the explanation was fully comprehensible.
Thank you very much! :slight_smile: