Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

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