x


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?

more ▼

asked Jan 03 '10 at 03:33 PM

Josh gravatar image

Josh
21 1 1 3

Could you provide some more information on what your current setup is vs. the desired one?

Jan 04 '10 at 10:44 AM AngryAnt ♦♦
(comments are locked)
10|3000 characters needed characters left

4 answers: sort voted first

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.

more ▼

answered Jan 19 '10 at 07:09 AM

Ashkan_gc gravatar image

Ashkan_gc
9k 33 56 117

I think this solution defeats the purpose of using Prefabs. The right way to do it is explained here by user "Slem."

Mar 25 at 09:08 PM swatmaster69
(comments are locked)
10|3000 characters needed characters left

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.
more ▼

answered Nov 16 '10 at 11:22 AM

nolver gravatar image

nolver
12 1 1 3

If you just instantiate the prefab and set the instance's parent, it'll work fine. The issue in the question was that he was trying to set the prefab's parent, not the instance's one

Nov 16 '10 at 11:59 AM Mike 3
(comments are locked)
10|3000 characters needed characters left

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)

more ▼

answered Nov 16 '10 at 01:44 PM

Proclyon gravatar image

Proclyon
1.4k 10 13 32

(comments are locked)
10|3000 characters needed characters left

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);
    }
}
more ▼

answered Jan 05 '10 at 05:07 AM

dkoontz gravatar image

dkoontz
170 6 8 18

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1250

asked: Jan 03 '10 at 03:33 PM

Seen: 6366 times

Last Updated: Mar 25 at 09:08 PM