Adding a Prefab Child to another Prefab using PrefabUtility

As many of you know, using PrefabUtility is vital to manipulate prefabs in editor scripts. The problem I am trying to automate today is adding many prefab Bs as children of another prefab A.

The problem is PrefabUtility.InstantiatePrefab(prefab B) only inserts prefab B into the current scene, while I want it to insert prefab B into a child of prefab A, while the prefab A is staged (open).

How can I accomplish this?

1 Answer

1

I’m not very good at prefabs but i think what you need to do is set the parent of prefab B as prefab A. I think the code should be close to…

PrefabUtlity.InstantiatePrefab(prefabB, prefabA.transform.position, Quaternion.Identity);

Ive never used the utility but i know this is how you spawn in a prefab and make it a child of another object. I’ve only ever done it as a normal prefab though. Give this a go and see if it works.