- Home /
Can I create a child gameobject on a prefab with an editor script
I'm writing my own custom inspector that I use only to edit prefabs and I want to be able to add children to the prefab. Currently I'm instantiating the prefab, adding children to the the instance, replacing the prefab with the instance, and finally deleting the instance. But that's horribly complex for what I'm trying to do. Am I just missing a basic function or is there no clean way to do this. Is there anything like:
GameObject PrefabUtility.AddNewChildGameObjectToPrefab(Object prefab);
That I'm just not finding?
Answer by Veehmot · Jun 12, 2013 at 04:42 PM
There is: AssetDatabase.AddObjectToAsset
Adds objectToAdd to an existing asset identified by assetObject.
Please note that you should only add assets to '.asset' files, imported models or texture assets for example will lose their data when reimporting or quitting Unity.
Answer by BG_Grill · Oct 19, 2021 at 12:50 AM
Hey, I've recently made a utility to tackle this problem and more. If you wish to use it then be sure to checkout my script on github. https://github.com/GameBrainDev/AddGameobjectfromscriptPrefabAsset