Search Unity

Changing object instance in custom editor does not apply modifications to prefab

Discussion in 'Prefabs' started by sient, Feb 1, 2019.

  1. sient

    sient

    Joined:
    Aug 9, 2013
    Posts:
    602
    I've been having issues with the new prefab workflow where changing the target value in an editor outside of SerializedObject does not apply modifications. In the past using EditorUtility.SetDirty(target) was enough to have the modifications apply.

    The below editor works correctly outside of prefab mode, but in prefab mode the change to Value (from pressing the button) is not written to disk.

    Any ideas? Thanks.

    Code (csharp):
    1. using UnityEditor;
    2. using UnityEngine;
    3.  
    4. public class Demo : MonoBehaviour {
    5.   public int Value;
    6. }
    7.  
    8. [CustomEditor(typeof(Demo))]
    9. class DemoEditor : Editor {
    10.   public override void OnInspectorGUI() {
    11.     if (GUILayout.Button("Generate new value")) {
    12.       ((Demo)target).Value = Random.Range(0, 10000);
    13.       EditorUtility.SetDirty(target);
    14.     }
    15.  
    16.     DrawDefaultInspector();
    17.   }
    18. }
     
  2. sient

    sient

    Joined:
    Aug 9, 2013
    Posts:
    602