• 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
Question by zico_mahilary · Nov 02, 2012 at 07:32 AM · transformparentchild

Setting the parent of a transform which resides in a prefab is disabled to prevent data corruption. WHY???

im using t$$anonymous$$s script to instantiate a game object. and im trying to make it a c$$anonymous$$ld of another game object named: level

 var brick : Transform;
 function Start () {
                 Instantiate(brick, Vector3 (0, 0, 10), Quaternion.identity);
                 brick.parent = transform;
                 }

I have attached the script to the game object : level.

but im getting t$$anonymous$$s error... "Setting the parent of a transform w$$anonymous$$ch resides in a prefab is disabled to prevent data corruption"

how to solve t$$anonymous$$s?

Comment
pajamajama
ocimum
felixmann
tylerwongj
yong2khoo
yektasarioglu

People who like this

6 Show 0
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

6 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by Shrandis · Nov 02, 2012 at 07:51 AM

 Instantiate(brick, Vector3 (0, 0, 10), Quaternion.identity);
 brick.parent = transform;

You are trying to set the parent of your brick PREFAB, not the actual brick you instantiated.

 GameObject myBrick = Instantiate(brick, Vector3 (0, 0, 10), Quaternion.identity) as GameObject;
 myBrick.transform.parent = transform;

Again, "brick" is your prefab, myBrick is the brick you instantiate here so you need to set it as c$$anonymous$$ld, NOT the prefab.

Comment
zico_mahilary
rutter
shahzad.naseem
pajamajama
EmperorCesar15
karl_
Hilfor
dappledore
gstryfe
CMS1
bigoMay
ForceMagic
BrandStone
grigdog
WalterCC
And 28 more...

People who like this

43 Show 4 · 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 zico_mahilary · Nov 02, 2012 at 09:35 AM 1
Share

awesome!!! thanks a lot.. cheers!

avatar image Bunny83 · Nov 02, 2012 at 10:20 AM 1
Share

The code in the question seems to be UnityScript. Your answer is (kind of C#). It's ok to post a C# solution, but it should be correct ;) "new Vector3(0, 0, 10)". Also the prefab was a Transform reference so the returned reference will also reference the Transform component of the created object. When you "as-cast" it to GameObject (which doesn't work) it will be null.

So either do this

 Transform brickPrefab;
 //[...]
 Transform myBrick = Instantiate(brickPrefab, new Vector3 (0, 0, 10), Quaternion.identity) as Transform;
 myBrick.parent = transform;

or

 Transform myBrick = (Transform)Instantiate(brickPrefab, new Vector3 (0, 0, 10), Quaternion.identity);
 myBrick.parent = transform;

in C#.

avatar image Nalincah · May 20, 2017 at 08:19 PM 0
Share

It doesn't work in both directions.

 GameObject foo = Instantiate(extra1, Vector3.zero, Quaternion.identity); 
 foo.transform.parent = otherPrefabGameObject;

You can neither set the parent of a prefab, nor set a prefab as a level. Both actions triggers the message "Setting the parent of a transform which resides in a prefab is disabled to prevent data corruption.". The Reason: A prefab is a file on your disk, and should never be modified by your Script

avatar image AibNihan · Feb 18, 2018 at 12:34 AM 0
Share

Thank you so much You Are Great It's Solve My Problem :)

avatar image

Answer by Cinnax · Jan 27, 2013 at 04:11 PM

I know t$$anonymous$$s is an older post, but the error "Setting the parent of a transform w$$anonymous$$ch resides in a prefab is disabled to prevent data corruption" indicates that you are attempting to parent a prefab. You cannot do so. When you instantiate an object from a prefab, you need to refer to the actual GameObject that was instantiated, not the prefab w$$anonymous$$ch the GameObject is cloned.

private GameObject objectPrefab; private GameObject prefabInstantiation; prefabInstantiation = Instantiate(objectPrefab, position, rotation) as GameObject; prefabInstantiation.transform.parent =

As you can see, I am not referring to the actual prefab, but the object w$$anonymous$$ch was instantiated. Then I am setting the instantiated objects parent as some other object.

Comment
zico_mahilary
Morfildor
Jeriko2k3
Wellfooled

People who like this

4 Show 0 · 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

Answer by Bryan-Legend · May 13, 2014 at 09:58 PM

T$$anonymous$$s error was happening to me in Unity 4.3.1 for no good reason.

Restarting Unity made the problem go away.

Comment
1d
leni8ec
BluishGreenPro

People who like this

3 Show 7 · 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 1d · Apr 26, 2015 at 09:01 PM 0
Share

This was the same at me.

avatar image tpainton · Oct 31, 2015 at 05:32 PM -1
Share

Yes, I feel there is a bug here.

 public void DisplayDown (List<HackingTarget> targets, Vector3 origin) {
 
         foreach (HackingTarget target in targets) {
             GameObject entry = (GameObject)Instantiate (textMesh, origin, Quaternion.identity);
             // Add the appropriate listener script.  
             entry.AddComponent<ScanClickLink> ();
             ScanClickLink clickscript = entry.GetComponent<ScanClickLink> ();
             clickscript.Target = target;
             entry.transform.parent = gameObject.transform;  // ERROR!!!
             entry.transform.localPosition = origin;
             TextMeshPro tMesh = entry.GetComponent <TextMeshPro> ();
             tMesh.text = BuildTargetDesc (target);
             tMesh.ForceMeshUpdate ();
             float newY = tMesh.mesh.bounds.center.y + tMesh.mesh.bounds.size.y + .2f;
             Vector3 temp = new Vector3 (0, newY, 0);
             origin -= temp;
         }
     }

The line "entry.transform.parent = gameObject.transform;" throws the error. Clearly entry is NOT the prefab. I have been trying to fix this issue for a week now. Restarting Unity does not fix the problem.

avatar image Bunny83 tpainton · Oct 31, 2015 at 07:52 PM 0
Share

This is not an answer. You should have posted a seperate question. Also are you sure you get the exact same error? Does this happen at runtime?

On what object is this script attached to and how do you call "DisplayDown"? Maybe you call this method on a prefab so "gameObject.transform" actually references a prefab?

Again, post a question and not an answer.

avatar image tpainton tpainton · Nov 01, 2015 at 03:23 PM 0
Share

Sorry, 10 years of stackoverflow.com moderaters telling me not to repeat questions and "This has already been asked". My problem seems to be the exact same. The question thus far, "restart Unity" isn't an answer.

So, from now on, if I See 3 questions that are similar to mine, all unanswered, the appropriate thing to do is post the question again?

I'll just do that now. If I don't get a response, I guess I'll be forced to come back here. Again, sorry. Probably didn't deserve a downvote. A simple notification that repetition is preferred would suffice.

reposted here http://answers.unity3d.com/questions/1091642/another-setting-the-parent-of-a-transform-which-re.html

avatar image Bunny83 tpainton · Nov 01, 2015 at 04:20 PM 0
Share

It seems to be the same? you're doing something totally different than what the OP does in his code. Also show me one SO moderator that suggest to ask a question in an answer. If it's a small follow up question it's ok to post a comment but never an answer.

"restart Unity" is almost never an answer. In some rare situations it might help. That's why that answer only has one upvote while the other two answers have 4 and 6 votes.

Well, you got the downvote because the answer you've posted doesn't answer the question.

avatar image tpainton · Nov 01, 2015 at 05:40 PM 0
Share

Yea, well I just converted my answer to a comment and flushed your down vote :) That's a nice feature.

avatar image tiagorpg · Mar 28, 2018 at 02:08 PM 0
Share

it is happening to me, but i have no idea where is it using a parent of a prefab, that error does not show the line were the problem occurs

avatar image

Answer by BrandStone · Dec 17, 2018 at 08:55 PM

T$$anonymous$$s problem appears when you try to instantiate the gameobject contained by another class:

 public class MyScript : MonoBehaviour {
     public Transform myTransform;

     void Awake () {
         GameObject myInstance = GameObject.Instantiate(myTransform.gameObject);
     }
 }
Comment
andywatts

People who like this

1 Show 0 · 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

Answer by gislersoft · Mar 08, 2021 at 10:03 PM

If you need to preserve original transformations use:

 GameObject prefab = Resources.Load<GameObject>(pathToPrefab);
 GameObject object = Instantiate(prefab) as GameObject;
 object.transform.SetParent(parentObject.transform);
 
 // Preserve original transformations
 object.transform.localPosition = prefab.transform.localPosition;
 object.transform.localRotation = prefab.transform.localRotation;
 object.transform.localScale = prefab.transform.localScale;
Comment
akast07

People who like this

1 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 florinel2102 · Mar 28, 2021 at 02:06 PM 0
Share

Thanks , I thought after Resources.load is finished the gameobject is instantiated .

  • 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

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Make a simple tree 1 Answer

find next(not necessarily immediate next) child of another parent in hierarchy with tag "checkpoint"? 1 Answer

How to make a child transform not move relative to parent? 1 Answer

Good way to scale parent without scaling children? 1 Answer

Transform.Find() hardwritten string works but string variable does not 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