Search Unity

CustomEditor variables no longer saving on prefabs in 2018.3

Discussion in 'Prefabs' started by RikN, Sep 19, 2018.

  1. RikN


    Sep 19, 2018
    Not sure if this is a bug or if the feature is simply removed/altered, but I couldn't find a similar issue on this board or the issue tracker and couldn't find what I was looking for in the release notes, so hopefully someone can help me on here. The following problem is present in the new beta and the 2018.2 improved prefab release: variables created in a CustomEditor script don't save their new values when their values are altered on the prefab.

    I have a CustomEditor for my GameManager object which allows me to set the width & height of my gamefield and have these changes be reflected in the Scene view directly. The code is relatively straightforward:
    Code (CSharp):
    1. [CustomEditor(typeof(GameManager))]
    2. public class BlockSizeEditor : Editor {
    3.     public override void OnInspectorGUI()
    4.     {
    5.         //draw the GameManager's default inspector
    6.         DrawDefaultInspector();
    8.         GameManager myScript = (GameManager)target;
    9.         //Add an option to set the size of the game
    10.         EditorGUILayout.HelpBox("Values that update in Sceneview:",MessageType.Info);
    11.         int gameW = EditorGUILayout.IntField("Game Width", myScript.gameWidth);
    12.         int gameH = EditorGUILayout.IntField("Game Height", myScript.gameHeight);
    14.         if (gameW != myScript.gameWidth || gameH != myScript.gameHeight)
    15.         {
    17.             //set the gameWidth & gameHeight variable in GameManager
    18.             myScript.gameWidth = gameW;
    19.             myScript.gameHeight = gameH;
    21.             myScript.setGameSize();
    22.         }
    23.    }
    24. }
    in the GameManager script 2 public variables are set:
    Code (CSharp):
    2.     [HideInInspector]
    3.     public int gameWidth;
    4.     [HideInInspector]
    5.     public int gameHeight;
    setGameSize() in GameManager basically moves and scales an object which represents the gamefield.

    In 2018.2 I was able to select my GameManager prefab, change the integer value for the game width or height and see these changes reflected immediately in the sceneview and gameview. In 2018.3 when I select the prefab the sceneview is not visible, but I still see the changes reflected in the game view. However, when I close the prefab and re-open it the changes are not saved!
    When I remove the [HideInInspector] block from the gameWidth and gameHeight variables in GameManager I can edit these values in the prefab as well. When I change these values no changes are made to the sceneview or gameview, but when I close and re-open the prefab the values are saved.

    I hope I was able to explain this problem properly. Any input or suggestions are appreciated.
  2. Mads-Nyholm


    Unity Technologies

    Aug 19, 2013
    When you are not using SerializedProperty you need to manually ensure the scene is dirty to ensure changes are saved. This is automatically handled if you use SerializedProperty and with that you also get proper undo handling at the same time.

    So either:
    1) change to use SerializedProperties for your custom editors (the recommended approach)
    2) ensure to call EditorSceneManager.MarkSceneDirty(myScript.gameObject.scene) after you have changed any properties on your script
  3. RikN


    Sep 19, 2018
    Thank you for your input! I was able to fix this by using your first suggestion.
    My code used to work in the 2018.2 version. So does this actually have anything to do with the new prefab setup from the beta?