ExecuteInEditMode Update() & Serialization not working

I’ve noticed that if I change the value of a serialized property in the Update() of a script that is marked as ExecuteInEditMode, even if I call Undo.Record(this, “Some changes”) or the SetSceneDirty function, Unity won’t save the changes and if I change scene or press play, it won’t stay the same value.

But, if I put the changes in the OnEnable() method, now it works (even without calling Undo.Record). I do not know if it is intended or a bug.

Here is a simple exemple.

using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
using System.Collections;

[ExecuteInEditMode]
public class TestUID : MonoBehaviour {

    [SerializeField]
    private string m_uid;

    // Update is called once per frame
    void Update ()
    {
        RegisterUID();
    }

    void OnEnable()
    {
        //RegisterUID();
    }
  
    private void RegisterUID()
    {
        if (Application.isPlaying)
            return;

#if UNITY_EDITOR
        if (string.IsNullOrEmpty(m_uid))
        {
            m_uid = System.Convert.ToBase64String(System.Guid.NewGuid().ToByteArray());
            Undo.RecordObject(this, "Register UID");
        }
#endif
    }

}
1 Like

Same here. I am surprised that no one replied to give their feedback on this issue.
I am running Unity on Mac

I know this question is old, but in case anyone finds this helpful:

EditorUtility.SetDirty(this);

^^^ use that to tell unity that you’ve made changes to your component so it can serialize them.