How do I set a parent of a prefab at runtime (C#)?

I tried transform.parent = desiredParentTransform

but unity gives me an error saying that parenting a prefab is disabled to prevent data corruption.

What do I do?

The “instantiated prefab” is useless. Just clone it and then use that. It’s just a weird thing about Unity.

For example, the following script … it loads one hundred “gems” by name from prefabs. Of course, you wouldn’t “drag in” 100 prefab references, so this takes care of it for you – getting them programatically by name and putting the 100 example gems in an array.

Note that where it makes “pf” … which is an “instantiated prefab” … that is useless.

You can do nothing with it.

The routine then just clones it – the GameObject thus made is totally normal and you can do what you want with it – so that’s “nu” … just forget about the “pf” object. (It will go away … I think :slight_smile: )

(Somewhat confusingly, in this game, whenever the game needs say a gem #71, it asks this script for a copy of that gem, so indeed it at that time clones “nu” number 71 and returns it. So, the 100 cloned "nu"s seen here are indeed only used themselves as models throughout gameplay.)

void Awake()
	{
	holder = new GameObject[11,11];
	
	for ( int shape = 1; shape <= 10; ++shape )
		for ( int color = 1; color <= 10; ++color )
			{
			string path = .. some complex file name ..
			GameObject pf = (GameObject)Resources.Load(path);
			// just completely forget about that thing, it's whacky
			
			GameObject nu = (GameObject)Instantiate(pf);
			
			nu.transform.parent = transform;
			nu.transform.localScale = blah...;
			nu.transform.localPosition = blah...;
			holder[ blah ] == nu;
			}
	}

you can not make a prefab parent of another and a prefab can not have a parent at all. in editor if you drag something on a prefab it will change the content of that prefab with the newly draged gameObject. if you want to change the parent of a prefab you should instantiate it and change the parent and then create a prefab from it again. in runtime you don't have access to editor classes and methods but in editor scripts you can do this easily. to create a prefab in a script you should use CreateEmptyPrefab and then assign a Gameobject to it by ReplacePrefab. so just instantiate the prefab, then do whatever you want. (add or remove components, change parent) and then apply to prefab.

Do you have a prefab that has not been instantiated yet? For example, do you have a class that has a public GameObject variable that you drag/drop the prefab onto? If so, you'll need to do one of two things (depending on how your game works). Either instantiate the prefab (http://unity3d.com/support/documentation/ScriptReference/Object.Instantiate.html) or create an instance of the prefab in your scene by dragging it into the scene and then dragging the prefab instanace in the scene onto your script. Here's an example of instantiating a prefab you've dragged onto your script that doesn't exist in the scene until runtime.

class AClassThatUsesAPrefab : MonoBehaviour {
    public GameObject myPrefab;
    public GameObject prefabInstance;

    void Start() {
        prefabInstance = (GameObject)Instantiate(myPrefab);
    }
}

The only workaround I know about this is the following:

  • At design-time create an empty object, and attach the prefab as a child of it. This object will act as a wrapper.
  • At run-time create instances of the wrapper object, instead of the prefab.
  • The instanced object transform parent can now be set to any other object.

I'f you'd let me reply the Quality in Q&A with Quality = q and Qq = Aq then I would say:

Just don't parent the prefab because it is a bad way to do what you want in any case. Make a clone with the Instantiate method Instantiate(PREFAB, TRANSFORM (pos), ROTATION)

Try this! Worked for me! Instantiate the prefab and use Game.Find to find it and add it to desired hierarchy. Like these:

class AClassThatUsesAPrefab : MonoBehaviour {
    public GameObject myPrefab;
    public GameObject prefabInstance;
 
    void Start() {
        prefabInstance = (GameObject)Instantiate(myPrefab);
        myPrefab.name = "NameOBJECT"; // if you have a multiple instances
        GameObject.Find("myPrefabNAME(Clone)").transform.parent = transform;

        myPrefab.name = "NameOBJECT2"; // if you have a multiple instances
        GameObject.Find("myPrefabNAME(Clone)").transform.parent = transform;

    }
}