Deleting null sub-assets

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?

2 Likes

As the notice indicates, there is an error in the script. You'll need to fix that first, check your console.

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.

1 Like

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.

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.


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).

1 Like

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

2 Likes

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.

@Rotary-Heart Did you know a way to do the same with prefabs?


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.

I leave a link here to forum with this problem: https://discussions.unity.com/t/792626
Maybe someone will meet it too.

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.

2 Likes

I've run into this issue as well. Since there was some confusion at the start of this thread, there is a very good explanation of the issue and the API limitations that cause it here:
https://discussions.unity.com/t/739349


you are brilliant dude, and also i love your work on serializable dictionary

1 Like

I am glad that it has been useful to you

[Serializable]
public class MainAsset : ScriptableObject
{
    [SerializeField] [HideInInspector] private List<Object> _references;

    public void AddSubAsset<TSubAsset> () where TSubAsset : ScriptableObject {
        var newSubAsset = CreateInstance<TSubAsset>();
        _references.Add(newSubAsset);
        AssetDatabase.AddObjectToAsset(newSubAsset, this);
        AssetDatabase.SaveAssets();
    }

    public void ValidateSubAssets () {
        var thisPath = AssetDatabase.GetAssetPath(this);
        var subAssets = AssetDatabase.LoadAllAssetsAtPath(thisPath);

        for (var i = subAssets.Length - 1; i >= 0; i--) {
           if(subAssets[i] != null) continue;

           AssetDatabase.RemoveObjectFromAsset(_references[i]);
           _references.RemoveAt(i);
        }

        AssetDatabase.SaveAssets();
    }
}