I’m trying to create a prefab of prefabs without losing all the connections.
Let me elaborate:
I have a barrel that is a mesh and some scripts assigned. So I create a prefab of it so I can place it throughout my levels easily.
I decide sometimes I want a group of stacked barrels sometimes instead of just one. So I create an empty game object that has 4 children that are my original barrel so I can easily place stacks of barrels throughout my levels.
However once I make a prefab of this empty game object I lose all the connection between the original barrel and the child barrel. This isn’t ideal as I’d still like the barrel children to be ‘barrels’ so if I add a script or change a value in the original I’d like any barrel anywhere to take this data… however since I’ve lost the link between the orignal this doesn’t happen.
Since I already had a copy of all my prefabs as well as having created a number of objects that contained versions of these prefabs, here is the code I ended up using:
public class NestedPrefabScript : MonoBehaviour {
public Transform[] m_NestedPrefabList;
// Use this for initialization
void Start () {
foreach (Transform child in transform)
{
foreach (Transform nestedPrefab in m_NestedPrefabList)
{
Debug.Log(child.name + " " + nestedPrefab.name);
if (child.name == nestedPrefab.name)
{
Vector3 childPos = child.position;
Quaternion childRot = child.rotation;
if (child.collider)
child.collider.enabled = false;
Destroy(child.gameObject);
Debug.Log("Replaced!");
Instantiate(nestedPrefab, childPos, childRot);
}
}
}
transform.DetachChildren();
Destroy(gameObject);
}
Caveats are that my parent object that contains all the nested objects was empty other than being a container for all my nested objects.
Worked for me. Hopefully someone else finds it remotely useful.