Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Delete button requires two clicks to completely remove item from array

Discussion in 'Immediate Mode GUI (IMGUI)' started by bixarrio, Oct 26, 2020.

  1. bixarrio

    bixarrio

    Joined:
    Dec 29, 2017
    Posts:
    18
    Hi,

    I have a ScriptableObject that contains an array of other ScriptableObjects. I am creating a custom editor for this parent scriptable object that allows me to add and remove from the array of children.

    upload_2020-10-26_23-42-56.png

    When I click the add button (+), I create a new instance of the child object, add it to the parent asset and insert it into the array. This works perfectly (except the property drawer is not working yet).

    upload_2020-10-26_23-44-2.png

    When I click the delete button (-) next to one of the children, I delete the child instance from the parent asset, destroy it and delete the entry from the array. This, however, does not work perfectly. The instance is removed from the parent asset, but the entry in the array remains, albeit null. I have to click the delete button a second time to get the entry removed from the array, too.

    The code to remove is pretty standard:

    Code (CSharp):
    1. if (GUILayout.Button("-", "miniButton", GUILayout.Width(20f)))
    2. {
    3.     var row = (DataRow)_children.GetArrayElementAtIndex(i).objectReferenceValue;
    4.     if (row != null)
    5.     {
    6.         AssetDatabase.RemoveObjectFromAsset(row);
    7.         DestroyImmediate(row, true);
    8.         AssetDatabase.SaveAssets();
    9.         AssetDatabase.Refresh();
    10.     }
    11.     _children.DeleteArrayElementAtIndex(i);
    12. }
    I'm not sure what's happening here. I have checked, and all the lines execute as expected. But the entry remains. I suspect there's a redraw before the AssetDatabase stuff is finished, but I'm not sure how this would cause my issue, and I have no idea how to go about doing what I want.

    Someone, please help!