Hello,

In a script with [ExecuteInEditMode], I tried to persistantly change a bool while in edit mode, doing

void Awake()
{
  #if UNITY_EDITOR
   if(!Application.isPlaying && condition)
   {
      myBool = false;
      UnityEditor.EditorUtility.SetDirty(this);
      //also tried
      //UnityEditor.EditorUtility.SetDirty(this as Component);
      //UnityEditor.EditorUtility.SetDirty(gameObject);
   }
  #endif
}

but the value change doesn’t get saved when I save the scene.
The class is derived from MonoBehaviour, the variable myBool is public. In fact, the value of the variable is saved correctly when I change it in the Inspector.

The question is now, is SetDirty only supposed to work in Editor-scripts, or should it work in this case, too?

Ok, seems like it does work if I call SetDirty in Start() rather than Awake()!
Thanks all!

Yes, set dirty should work in any script.

Your problem is that ExecuteInEditMode doesn’t call Awake:

No UnityEditor functionality is supposed to be used in any runtime/ingame classes such as a custom component or class derived from monobehaviour or scriptableobject. So any UnityEditor functionality is not guaranteed to properly work in any of these classes as well as you will get big problems when trying to build your game as the UnityEditor namespace is stripped out which will then cause compiler errors of any runtime classes using the UnityEditor namespace and the build will fail. Although you did include the Unity_Editor define, so you are good there.

To achieve what you want you should create a custom inspector for your component and use the SetDirty function with it.