• 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
0

Answer by dkoontz · Jan 05, 2010 at 05:07 AM

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

}

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

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
-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;

  • 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