Search Unity

AssetDatabase.AddObjectToAsset doesn't seem to work with ScriptableObjects

Discussion in 'Asset Database' started by Stephan-B, May 23, 2013.

  1. Stephan-B

    Stephan-B

    Joined:
    Feb 23, 2011
    Posts:
    2,269
    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.

    $AddToObject Bug.JPG

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

    $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?
     
    Last edited: May 23, 2013
  2. Stephan-B

    Stephan-B

    Joined:
    Feb 23, 2011
    Posts:
    2,269
    Is this a bug or am I missing something here?
     
  3. Stephan-B

    Stephan-B

    Joined:
    Feb 23, 2011
    Posts:
    2,269
    Just updating this thread with additional information.

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

    Before
    $AddObjectToAsset Step 1.JPG

    After
    $AddObjectToAsset Step 2.JPG
     
  4. FlaSh-G

    FlaSh-G

    Joined:
    Apr 21, 2010
    Posts:
    212
    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?
     
  5. Stephan-B

    Stephan-B

    Joined:
    Feb 23, 2011
    Posts:
    2,269
    Filed a bug report over 2 months ago but haven't heard anything yet :(
     
    Last edited: Sep 24, 2013
  6. Stephan-B

    Stephan-B

    Joined:
    Feb 23, 2011
    Posts:
    2,269
    Would it be possible to get an update from the Unity Gods on this?
     
  7. FlaSh-G

    FlaSh-G

    Joined:
    Apr 21, 2010
    Posts:
    212
    My last info: The bug has been reproduced, the ticket's still open.
     
    Last edited: Aug 28, 2014
  8. Graham-Dunnett

    Graham-Dunnett

    Administrator

    Joined:
    Jun 2, 2009
    Posts:
    4,287
    QA team have reproduced this problem, and it's with Devs for a fix.
     
  9. Stephan-B

    Stephan-B

    Joined:
    Feb 23, 2011
    Posts:
    2,269
    Thanks
     
  10. rakkarage

    rakkarage

    Joined:
    Feb 3, 2014
    Posts:
    683
    any fix?
    any news?
    any workaround?
    or working as intended?
    thanks
     
  11. Graham-Dunnett

    Graham-Dunnett

    Administrator

    Joined:
    Jun 2, 2009
    Posts:
    4,287
    No news, it's sat with Devs for over a year. :-(
     
  12. rakkarage

    rakkarage

    Joined:
    Feb 3, 2014
    Posts:
    683
    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?
     
  13. Darkcoder

    Darkcoder

    Joined:
    Apr 13, 2011
    Posts:
    3,414
    Still not fixed? wtf !?
     
  14. DarkMeatGames

    DarkMeatGames

    Joined:
    Jan 11, 2014
    Posts:
    5
    Still having this problem. Can I go somewhere to vote on this being fixed soon?
     
  15. Shorodei

    Shorodei

    Joined:
    Dec 7, 2013
    Posts:
    8
    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.
     
  16. Shorodei

    Shorodei

    Joined:
    Dec 7, 2013
    Posts:
    8
  17. kromenak

    kromenak

    Joined:
    Feb 9, 2011
    Posts:
    270
    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.
     
    rakkarage and winxalex like this.
  18. winxalex

    winxalex

    Joined:
    Jun 29, 2014
    Posts:
    166
    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)
     
    Last edited: Apr 16, 2016
    rakkarage likes this.
  19. bitbiome_llc

    bitbiome_llc

    Joined:
    Aug 3, 2015
    Posts:
    58
    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.
     
    Last edited: Apr 20, 2016
  20. Teh_K

    Teh_K

    Joined:
    Feb 9, 2014
    Posts:
    3
    FYI, setting the ScriptableObject name didn't work for me in 5.3.3

    The HideInHierarchy hack did, tho.
     
  21. Immanuel-Scholz

    Immanuel-Scholz

    Joined:
    Jun 8, 2013
    Posts:
    221
    I am pretty sure this is not really a bug, but more a "quirk" about how objects are stored and retrieved in assets.

    I talked to Richard Fine (@superpig) about this on Unite Europe and the main problem is, that there is currently no way of identifying the "main asset". Both objects inside the same file are equally capable of being the "main" asset, so Unity groups them together under one empty icon.

    For the same reason, you can't just add a GameObject to another GameObject asset (because the "main asset" on an asset file containing any GameObject is defined as "the GameObject, that has no parent")

    Cheers,
    Imi.
     
  22. Immanuel-Scholz

    Immanuel-Scholz

    Joined:
    Jun 8, 2013
    Posts:
    221
    Yes, any "HideInHierarchy" is per-Unity-Definition never a main asset, so if there is only one asset inside a file that has not HideInHierarchy, that one is the main asset. ;)
     
  23. msklywenn

    msklywenn

    Joined:
    Nov 28, 2014
    Posts:
    67
    I use AssetDatabase.AddObjectToAsset to add animations to animation controllers (to avoid having that many animation files...). Worked fine with 5.4, now with 5.5, it still works, it just doesn't show up in the project window until we save the project. It's kind of annoying...
     
  24. RubenPineda

    RubenPineda

    Joined:
    Mar 5, 2018
    Posts:
    1
    AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(path));
    is your friend here.
     
    Last edited: Mar 11, 2018
  25. Rodolfo-Rubens

    Rodolfo-Rubens

    Joined:
    Nov 17, 2012
    Posts:
    1,197
    I'm currently having this bug, the asset that is supposed to be the root simple becomes a child of a random asset that was supposed to be its child instead when I hit save... Version 2019.4.7f1.
     
    Last edited: Sep 11, 2020
  26. Stacklucker

    Stacklucker

    Joined:
    Jan 23, 2015
    Posts:
    82
    bumping this thread
     
  27. Maeslezo

    Maeslezo

    Joined:
    Jun 16, 2015
    Posts:
    332
    Same here. AssetDatabase.AddObjectToAsset doesn't work with scriptable objects