• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Josh · Jan 03, 2010 at 03:33 PM · prefab

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?

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Fattie · Mar 25, 2015 at 04:00 PM 0
Share

What you do is... Load the prefab, and then using that as a model Instantiate yet another gameObject based on that. I than just forget about the "loaded-prefab" thing, which is silly and useless. You then finally have a totally normal game object, that you can (obviously and normally) put in a hierarchy, name, change, whatever. It's just a weird thing about Unity.

6 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Ashkan_gc · Jan 19, 2010 at 07:09 AM

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image swatmaster69 · Mar 25, 2013 at 09:08 PM 0
Share

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

avatar image Fattie · Mar 25, 2015 at 04:02 PM 0
Share

yeah this is not the way to go. simply, clone the 'instantiated prefab' and use that normally in the normal way. the actual 'instantiated prefab' is useless.

avatar image
5

Answer by Fattie · Mar 25, 2015 at 04:09 PM

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 :) )

(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;
             }
     }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image TooManySugar · Dec 06, 2015 at 10:42 PM 0
Share

yep! that works!!!! thanks a lot!

avatar image cosmochristo · Oct 09, 2020 at 02:18 AM 0
Share

this helped me, thanks!. I cannot understand how prefabs can be useful without the ability to load and place with scripts. Should be directly supported.

avatar image
-2

Answer by ThiagoVT · Sep 18, 2013 at 12:07 PM

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;
 
     }
 }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Sep 18, 2013 at 12:37 PM 1
Share

Sorry, but that makes no sense. prefabInstance is already the instantiated object. Using GameObject.Find is just slow and gives you much trouble if you have multiple objects with the same name.

avatar image ThiagoVT · Sep 18, 2013 at 02:02 PM 0
Share

sorry but worked perfectly fine for me, and i solved the multiples instances using myPrefab.name, and not happens slowly for me... myPrefab.name = "PrefabName"; GameObject.Find("myPrefabNA$$anonymous$$E(Clone)").transform.parent = transform;

avatar image
0

Answer by Proclyon · Nov 16, 2010 at 01:44 PM

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)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by nolver · Nov 16, 2010 at 11:22 AM

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Mike 3 · Nov 16, 2010 at 11:59 AM 1
Share

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

  • 1
  • 2
  • ›

Your answer

Hint: You can notify a user about this post by typing @username

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

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Tower defense target update 0 Answers

Detecting if a certain tag near another tag? 1 Answer

Swipe to place order in ShopScrollList (prefabs, object pooling, instantiating) 0 Answers

How do i access a network instantiated object? 1 Answer

Apply Changes to Prefab - Unity 2018 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges