Search Unity

AssetDatabase.AddObjectToAsset is working BUT I get a Assertion failed on expression

Discussion in 'Asset Database' started by fwalkerCirca, Dec 31, 2018.

  1. fwalkerCirca

    fwalkerCirca

    Joined:
    Apr 10, 2017
    Posts:
    57
    The exception is: '!(o->TestHideFlag(Object::kDontSaveInEditor) && (options & kAllowDontSaveObjectsToBePersistent) == 0)'

    Here is my code where I call the AddObjectAsset:
    Code (CSharp):
    1.  
    2.  
    3.            ContentLibrary contentLibrary = target as ContentLibrary;
    4.             MyAsset newAsset = null;
    5.  
    6.             if ((Type)anObjectType == typeof(MyAssetTexture))
    7.             {
    8.                 newAsset = ScriptableObject.CreateInstance<MyAssetTexture>();
    9.                 contentLibrary.Assets.Add(newMyAsset);
    10.             }
    11.             else if ((Type)anObjectType == typeof(MyAssetText))
    12.             {
    13.                 newAsset = ScriptableObject.CreateInstance<MyAssetText>();
    14.                 contentLibrary.Assets.Add(newAsset);
    15.             }
    16.          
    17. if( newAsset != null)
    18. {
    19.             AssetDatabase.AddObjectToAsset(newAsset,contentLibrary);
    20. }
    21.  
    22.  
    ContentLibrary is a ScriptableObject with hideFlags = HideFlags.DontSave;

    MyAsset is a ScriptableObject with hideFlags = HideFlags.HideAndDontSave;

    MyAssetTexture is a subclass of MyAsset
    MyAssetText is a subclass of MyAsset
    Their flags are defined by the base class MyAsset so hideFlags = HideFlags.HideAndDontSave;

    I know this is going to end up being something really silly.

    NOTE: I am working with a reordableList, I could not find a way to add asset to the actual reordablelist. So I intercepted the click method and added the object directly to the instances list (Assets)...
    So on the editor it seems to be drawing the base class MyAsset with nothing in it (an empty element) and on top of it the actual derived class MyAssetTexture or MyAssetTex with the correct drawing as defined by a ContentLibraryEditor class.
    No idea if there is a relation to the problem or not.
     
    Last edited: Dec 31, 2018
  2. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,609
  3. fwalkerCirca

    fwalkerCirca

    Joined:
    Apr 10, 2017
    Posts:
    57
    Uhhh. wow... Slightly speechless :(