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?

1 Like

6 Answers

6

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.

awesome!!! thanks a lot.. cheers!

–

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

–

This error was happening to me in Unity 4.3.1 for no good reason.

Restarting Unity made the problem go away.

This was the same at me.

–

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

–

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.

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

Hello.
I have a similar problem.

I got the “Fish.cs” and “GlobalFlock.cs” files from here: FishFlocker/Assets/Scripts at master · Streamweaver/FishFlocker · GitHub

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 = fish;
Thanks for your help.

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;

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

–