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

Bug [Editor Scripting] Cannot destroy nested scriptable objects?

Discussion in 'Scripting' started by Raul_T, Jun 15, 2021.

  1. Raul_T

    Raul_T

    Joined:
    Jan 10, 2015
    Posts:
    363
    Hello.

    As the title says I am having trouble deleting scriptable objects that are nested in a main object in Unity 2019.4.26f1

    This is the code I am trying to use for destroying the objects, however it doesn't seem to work correctly.

    Code (CSharp):
    1. for (int i = action.Count - 1; i >= 0; i--) {
    2.     Object.DestroyImmediate(action[i], true);
    3. }
    4.  
    5. UnityEditor.EditorUtility.SetDirty(this);
    6. UnityEditor.AssetDatabase.ImportAsset(UnityEditor.AssetDatabase.GetAssetPath(this));
    7. UnityEditor.AssetDatabase.SaveAssets();
    The asset instance seem to get destroyed (inspector of scriptable object is blank) but the asset itself remains in hierarcy.

    upload_2021-6-15_12-37-58.png

    Also upon reimporting the asset or if I reopen the project it seems to be de-serialized from file back to it's original state.

    upload_2021-6-15_12-38-48.png

    Is it something wrong with my code snipped or is this a serialization bug? Not really a critical issue since we can just ignore the old assets but it really bloats the asset selection panels so it gets annoying. Should we file a bug report?
     
  2. MostHated

    MostHated

    Joined:
    Nov 29, 2015
    Posts:
    1,235
    Give this a go.


    Code (CSharp):
    1. for (int i = action.Count - 1; i >= 0; i--) {
    2.     AssetDatabase.RemoveObjectFromAsset(action[i]);
    3.     Object.DestroyImmediate(action[i], true);
    4. }
    5.  
    6. UnityEditor.EditorUtility.SetDirty(this);
    7. UnityEditor.AssetDatabase.ImportAsset(UnityEditor.AssetDatabase.GetAssetPath(this));
    8. UnityEditor.AssetDatabase.SaveAssets();
    You might need to change the last bit as well, I have it like this and works just fine for me:

    Code (CSharp):
    1. AssetDatabase.RemoveObjectFromAsset(layerTagObj);
    2. DestroyImmediate(layerTagObj);
    3.  
    4. AssetDatabase.SaveAssets();
    5. AssetDatabase.Refresh();
     
  3. Raul_T

    Raul_T

    Joined:
    Jan 10, 2015
    Posts:
    363
    Thanks! Still the same outcome though :(
     
  4. MostHated

    MostHated

    Joined:
    Nov 29, 2015
    Posts:
    1,235
    I would try removing the ImportAsset line. I believe you are reimporting the asset as it was before actually saving the changes that you made. I would try it just like I have it in the second code section and see if there is a difference in outcome.
     
  5. Raul_T

    Raul_T

    Joined:
    Jan 10, 2015
    Posts:
    363
    Oh that was it! Thanks a ton!

    P.S. Oh god that makes perfect sense to why it didn't work, how did I missed it :D
     
    MostHated likes this.