- Home /
Nested ScriptableObject assets?
I'm having a hard time with nested ScriptableObjects. I've tried everything I can think of, but I can't seem to add a ScriptableObject asset to another one. Here's one of the many things I've tried:
private ChildClass AddNewChild (Object database) {
ChildClass newChild = CreateInstance<ChildClass> (); //Derives from ScriptableObject
newChild.hideFlags = HideFlags.HideInHierarchy;
AssetDatabase.CreateAsset (newChild, AssetDatabase.GenerateUniqueAssetPath ("Assets/New Child.asset"));
AssetDatabase.AddObjectToAsset (newChild, database); //Database is also a ScriptableObject asset.
AssetDatabase.SaveAssets ();
AssetDatabase.Refresh ();
return newChild;
}
No matter what I do or how I call or arrange these lines, it always throws an error at the AddObjectToAsset function. "Couldn't add object to asset file because the MonoBehaviour 'New Child' is already an asset at 'Assets/New Child.asset'!
I could have sworn I was able to nest ScriptableObjects before... What am I doing wrong?
Answer by Sprakle · Apr 07, 2016 at 06:58 PM
You should remove line 6, objects are supposed to be added to assets without making assets from them in the first place.
You also need to set the name of newChild to something different before adding it. Otherwise things will get very weird.
Your answer
![](https://koobas.hobune.stream/wayback/20220612065441im_/https://answers.unity.com/themes/thub/images/avi.jpg)