Search Unity

serializedObject.Update(); resets object's values after asset creation is confirmed

Discussion in 'Editor Workflows' started by UniOwl, Feb 12, 2023.

  1. UniOwl

    UniOwl

    Joined:
    May 31, 2018
    Posts:
    24
    I have an object deriving from ScriptableObject and custom editor script for it. When I start to create asset, editor seems to work fine (creation is not confirmed yet as I can rename asset or cancel creation):
    upload_2023-2-13_1-7-18.png

    But as soon as I confirm asset creation, fields reset: upload_2023-2-13_1-7-50.png

    Part of custom editor:
    Code (CSharp):
    1.  
    2. public override void OnInspectorGUI()
    3.     {
    4.         serializedObject.Update();
    5.      
    6.         // Draw some stuff
    7.  
    8.         serializedObject.ApplyModifiedProperties();
    9.     }
    I found out that
    serializedObject.Update()
    causes the object to reset. If I remove that line, fields stay the same after asset creation, but I can't change its fields after.

    But after next recompilation object calls it's
    OnEnable()
    and fields appear back. CustomEditor don't reset them again.

    How can I fix that?