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

Deleting null sub-assets

Discussion in 'Scripting' started by jesta, Apr 15, 2017.

  1. jesta

    jesta

    Joined:
    Jun 19, 2010
    Posts:
    293
    I added several objects to a main asset, but after some refactoring, one of them is now null.



    I tried calling DestroyImmediate() on this asset but it does nothing. If I get the path through AssetDatabase, I get an empty string.

    How am I supposed to delete this asset?
     
    TobyWu and phobos2077 like this.
  2. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    9,042
    As the notice indicates, there is an error in the script. You'll need to fix that first, check your console.
     
  3. jesta

    jesta

    Joined:
    Jun 19, 2010
    Posts:
    293
    I know there is an error, that's the case I need to handle. If the script cannot compile or is missing, I need to delete the sub-asset.
     
    phobos2077 likes this.
  4. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    9,042
    If there is an error like that, it isn't a valid object, so you can't destroy it. You need to manually delete the file, or fix the error.
     
  5. makeshiftwings

    makeshiftwings

    Joined:
    May 28, 2011
    Posts:
    3,350
    Are you just asking how to delete it in the inspector? You can click the little gear in the upper right corner and choose "Remove Component" even if it's a missing script.
     
  6. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    9,042
    It's a nested asset, presumably a scriptable object. To remove them (or add them), it's done via editor script. It's an asset inside another asset, serialzed. But if the code is changed in a way that breaks it or is removed, the asset is broken, so editor scripts can't really do anything with it. (Other than delete the parent via IO).
     
    Joe-Censored likes this.
  7. Rotary-Heart

    Rotary-Heart

    Joined:
    Dec 18, 2012
    Posts:
    813
    Reviving this thread since the issue is still present, even Unity can't delete the object since if you right click it the delete option will be disabled. Your only options would be to add back the old script used to create the asset or manually edit the .asset text. I found a workaround for this that I've uploaded here: https://gitlab.com/RotaryHeart-UnityShare/subassetmissingscriptdelete
     
    MichealChiNguyen and phobos2077 like this.
  8. Camarent

    Camarent

    Joined:
    Feb 19, 2014
    Posts:
    168
    I actually have the same problem with prefabs but using this script cause to regenerate guids and i am loosing link to this prefabs. I also don't know what will happen with variants and nested if i use it. Right now only way I see is to delete this directly from yaml.
     
  9. Camarent

    Camarent

    Joined:
    Feb 19, 2014
    Posts:
    168
    @Rotary-Heart Did you know a way to do the same with prefabs?
     
  10. Rotary-Heart

    Rotary-Heart

    Joined:
    Dec 18, 2012
    Posts:
    813
    Unfortunately no, haven't had the issue with prefabs before. But the workaround that I found is doing exactly that so that might be your only way to go.
     
  11. Camarent

    Camarent

    Joined:
    Feb 19, 2014
    Posts:
    168
  12. ErnestSurys

    ErnestSurys

    Joined:
    Jan 18, 2018
    Posts:
    94
    I have just encountered the same problem. Sub-assets are a cool concept but also are really painful to use. No option to rename/delete/move from the editor and now this.
     
    phobos2077 and MUGIK like this.
  13. AndyKorth

    AndyKorth

    Joined:
    Oct 19, 2009
    Posts:
    41
  14. MichealChiNguyen

    MichealChiNguyen

    Joined:
    Nov 16, 2020
    Posts:
    5
    you are brilliant dude, and also i love your work on serializable dictionary
     
    Rotary-Heart likes this.
  15. Rotary-Heart

    Rotary-Heart

    Joined:
    Dec 18, 2012
    Posts:
    813
    I am glad that it has been useful to you
     
  16. Toolsmith

    Toolsmith

    Joined:
    Sep 29, 2016
    Posts:
    9
    Code (CSharp):
    1. [Serializable]
    2. public class MainAsset : ScriptableObject
    3. {
    4.     [SerializeField] [HideInInspector] private List<Object> _references;
    5.  
    6.     public void AddSubAsset<TSubAsset> () where TSubAsset : ScriptableObject {
    7.         var newSubAsset = CreateInstance<TSubAsset>();
    8.         _references.Add(newSubAsset);
    9.         AssetDatabase.AddObjectToAsset(newSubAsset, this);
    10.         AssetDatabase.SaveAssets();
    11.     }
    12.      
    13.     public void ValidateSubAssets () {
    14.         var thisPath = AssetDatabase.GetAssetPath(this);
    15.         var subAssets = AssetDatabase.LoadAllAssetsAtPath(thisPath);
    16.  
    17.         for (var i = subAssets.Length - 1; i >= 0; i--) {
    18.            if(subAssets[i] != null) continue;
    19.              
    20.            AssetDatabase.RemoveObjectFromAsset(_references[i]);
    21.            _references.RemoveAt(i);
    22.         }
    23.          
    24.         AssetDatabase.SaveAssets();
    25.     }
    26. }