AssetDatabase.AddObjectToAsset doesn't seem to work with ScriptableObjects

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.

1252942--54370--$AddToObject Bug.JPG

Adding sub-objects to a Material or Texture or a .fontsettings object works as expected.

1151221--43801--$AddObjectToAsset 2.JPG

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

Can anyone else confirm this?

Is this a bug or am I missing something here?

Just updating this thread with additional information.

As per the attached pictures, AssetDatabase.AddObjectToAsset doesn’t work either with objects Animation clips either.

Before
1254752--54614--$AddObjectToAsset Step 1.JPG

After
1254752--54615--$AddObjectToAsset Step 2.JPG

Having the same issue here.

I’m trying to add a Texture2D to a ScriptableObject.
No matter what I do, the best thing I can get is the ScriptableObject added to the Texture.

Any news on this?

Filed a bug report over 2 months ago but haven’t heard anything yet :frowning:

Would it be possible to get an update from the Unity Gods on this?

My last info: The bug has been reproduced, the ticket’s still open.

QA team have reproduced this problem, and it’s with Devs for a fix.

Thanks

any fix?
any news?
any workaround?
or working as intended?
thanks

No news, it’s sat with Devs for over a year. :frowning:

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?

Still not fixed? wtf !?

Still having this problem. Can I go somewhere to vote on this being fixed soon?

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.

Here’s the tracking issue if you want to vote it up.

@DarkMeatGames

1 Like

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.

2 Likes

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)

1 Like

This still appears to be an issue in U5.3.4f1.

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.

FYI, setting the ScriptableObject name didn’t work for me in 5.3.3

The HideInHierarchy hack did, tho.