AssetDatabase.IsSubAsset and HideFlags Questions

Hello.

So I have a sub-asset called [some_sub_asset] which is a part of [main_asset]. I set the hideflag for [some_sub_asset] as “HideInHierarchy”, so it looks nicer in the hierarchy window (so it shows only the main asset, instead of having a whole list of subassets under the main asset).

But when I run the following code when the hideflag of [some_sub_asset] is “HideInHierarchy”, Unity throws an error saying the sub-asset already exists in the main asset.

if (AssetDatabase.IsSubAsset(some_sub_asset) == false)
   AssetDatabase.AddObjectToAsset(some_sub_asset, main_asset);

It seems as if IsSubAsset(some_sub_asset) fails if the subassets hideflag is set as “HideInHierarchy”. When I set the hideflag as “None” it works as intended as IsSubAsset(some_sub_asset) returns true.

Since I simply wanted to change the hideflag for aesthetic purposes, I can just turn the hidelfag off and work around it but I was just wondering if anybody knew why Unity would behave this way.

Thank you!

It’s a little bit of a hack, but AssetDatabase.GetAssetPath() will return the same path for both assets in this case. So you can check if AssetDatabase.GetAssetPath(some_sub_asset) == AssetDatabase.GetAssetPath(main_asset) and, if so, you’ve got a sub asset.