- Home /
Setting the parent of a transform which resides in a prefab is disabled to prevent data corruption. WHY???
im using this script to instantiate a game object. and im trying to make it a child 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 this error... "Setting the parent of a transform which resides in a prefab is disabled to prevent data corruption"
how to solve this?
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 child, NOT the prefab.
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#.
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
Thank you so much You Are Great It's Solve $$anonymous$$y Problem :)
Answer by Cinnax · Jan 27, 2013 at 04:11 PM
I know this is an older post, but the error "Setting the parent of a transform which 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 which 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 which was instantiated. Then I am setting the instantiated objects parent as some other object.
Answer by Bryan-Legend · May 13, 2014 at 09:58 PM
This error was happening to me in Unity 4.3.1 for no good reason.
Restarting Unity made the problem go away.
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 (text$$anonymous$$esh, 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;
Text$$anonymous$$eshPro t$$anonymous$$esh = entry.GetComponent <Text$$anonymous$$eshPro> ();
t$$anonymous$$esh.text = BuildTargetDesc (target);
t$$anonymous$$esh.Force$$anonymous$$eshUpdate ();
float newY = t$$anonymous$$esh.mesh.bounds.center.y + t$$anonymous$$esh.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.
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"? $$anonymous$$aybe you call this method on a prefab so "gameObject.transform" actually references a prefab?
Again, post a question and not an answer.
Sorry, 10 years of stackoverflow.com moderaters telling me not to repeat questions and "This has already been asked". $$anonymous$$y 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 $$anonymous$$e, 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
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.
Yea, well I just converted my answer to a comment and flushed your down vote :) That's a nice feature.
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
Answer by BrandStone · Dec 17, 2018 at 08:55 PM
This 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);
}
}
Answer by christDuNord · Nov 15, 2020 at 11:21 AM
Hello. I have a similar problem.
I got the "Fish.cs" and "GlobalFlock.cs" files from here: https://github.com/Streamweaver/FishFlocker/tree/master/Assets/Scripts
It works very well in Unity windows, but when I create the build for my Oculus Quest 2, the fish do not appear. In the console I have this message:
Setting the parent of a transform which resides in a Prefab Asset is disabled to prevent data corruption (GameObject: 'Needlenose Fish (Clone)'). UnityEngine.Transform: set_parent (Transform) GlobalFlock: Start () (at Assets / GlobalFlock.cs: 28)
Knowing that if I put the fish directly on the stage, I can see them clearly with my Quest!
Original code :
26 GameObject fish = (GameObject)Instantiate(fishPrefabs[Random.Range(0, fishPrefabs.Length)], pos, Quaternion.identity);
27 fish.transform.parent = fishSchool.transform;
28 allFish[i] = fish;
Thanks for your help.
Your answer
Follow this Question
Related Questions
Make a simple tree 1 Answer
Make a GameObject child to a "Dragged Reference" GameObject doesn't work! 0 Answers
Changing child's rotation changes parent's position 0 Answers
Properly Rotating Child Objects by Script 1 Answer
Creating new Transform from existing objects Transform to Instantiate object 1 Answer