- Home /
Can I make Prefabs that contain other Prefabs?
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.
Is there a better way to do this?
No. Unity doesn't support nested prefabs as of yet, although it's on the roadmap. When it will be available is subject to great mystery.
Answer by save · Dec 29, 2011 at 07:44 AM
Nested prefabs is on the wishlist, go vote. ;)
What you could do to maintain your working structure is to have placeholders in the level, then in Awake() or Start() instantiate them.
function Awake () {
var placeholders : GameObject[] = GameObject.FindGameObjectsWithTag("BarrelSpawn")
for (var placeholder in placeholders) {
Instantiate (barrelPrefab, placeholder.transform.position, placeholder.transform.rotation);
}
}
Answer by Bento-Studio · Mar 12, 2012 at 02:07 PM
I you are still interested by nested prefabs in prefabs, I have created an editor extension : http://u3d.as/content/bento-studio/nested-prefab/2Jz
This extension allows you to make prefabs that contains other prefabs and so on.
There is no need for such a solution. Unity3D 3.5 made the first step to include nested prefabs (you can add or remove components from prefab dragged into the scene without breaking the prefab), which is a first step for a built-in nested prefab solution
Answer by JoshOClock · Dec 30, 2011 at 04:41 AM
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.
Your answer
Follow this Question
Related Questions
Count Prefabs on builded .apk 0 Answers
Multiplayer different player prefab 0 Answers
PrefabUtility does not exist? 1 Answer
how can i make prefabs keep there inspector varibles 1 Answer
(Re-posted) How to save multiple of the same prefab clones 0 Answers