Adding sub-objects (a material, texture, animation clip, etc…) to a ScriptableObject using AssetDatabase.AddObjectToAsset doesn’t seem to work. As you can see in the attached image, both the ScriptableObject and in this case an animation clip are instead added to a new empty object.
Adding sub-objects to a Material or Texture or a .fontsettings object works as expected.
According to the documentation, “Please note that you should only add assets to ‘.asset’ files” … ScriptableObjects are .asset files and this doesn’t work (for me anyway).
i guess it is easier to manage separate files in folders anyway
but thousands!?
should i use this broken & forgotten code to transform my assets into malformed anomalies or just use files?
Wow this is a bug?! I thought that was how it was meant to work.
It still kind of works, except my last added item somehow loses all data when I press “Play” on the editor.
Not sure if this would be a workable solution for everyone, but I’ve found that if I set the hideFlags for the object to be embedded to “HideInHierarchy”, everything seems to work correctly.
I had a scenario where ScriptableObject1 had a list of another ScriptableObject2. Instead of having hundreds of ScriptableObject2 assets lying around, I wanted to embed them directly into ScriptableObject1. Using AddObjectToAsset, I found that as reported above, the hierarchy shows up incorrectly, and I couldn’t change the name of the asset.
But if I set ScriptableObject2’s hideFlags to HideInHierarchy, it draws correctly in the project view and doesn’t throw any weird errors. I can verify that ScriptableObject2 is being successfully embedded in ScriptableObject1 by opening the ScriptableObject1.asset file and seeing the ScriptableObject2 data in there.
This works for my needs, but of course a downside is that the inner asset doesn’t show up in the project view. For my purposes, that’s fine.
Not resolved in U5.3.3p2. Shame on you. For me this if no Top priority is High priority. Why is closed? I’ve come to same solution as @kromenek (HideFlags.HideInHierachy before add and losing show up in Project view, which is very important when u developing somthing to check, u not do grabage)
I created a ScriptableObject container and saved it to an .asset. I have another ScriptableObject class. Using CreateInstance I then add this new ScrtiableObject to the container using AddObjectToAsset. The hierarchy is all messed up in the editor. HideInHierarchy at least keeps the container visible at the root. It looked like there was a “fix” back in 5.3.3p1. But it isn’t working in the version I’ve listed in this post.
[EDIT]
Apparently there is still a bug but it is related to the ScriptableObject name. If the object has no name before using AddObjectToAsset the hierarchy will become messed up. If you give the object a name the before using AddObjectToAsset the hierarchy works.