- Home /
Setting parent causes prefab error
The full error is:
Setting the parent of a transform which resides in a prefab is disabled to prevent data corruption
I have googled and most of the instances of others facing this issue seem to be related to them directly accessing a prefab. However, as you can see in the code below, I am instantiating an prefab and directly accessing a gameObject in the scene. Anyone got any ideas?
public void UpdateMesh(GameObject newMesh, GameObject skinnedRoot) { GameObject newBody = GameObject.Instantiate(newMesh); GameObject oldBody = gameObject.transform.GetChild(0).gameObject; newBody.transform.parent = oldBody.transform.parent; //Setting parent error newBody.transform.position = oldBody.transform.position; newBody.transform.rotation = oldBody.transform.rotation; newBody.transform.localScale = oldBody.transform.localScale; GameObject.Destroy(oldBody); //Destroying asset not permitted error _bodyMesh = skinnedRoot; _animator = newBody.GetComponent<Animator>(); _animationObject = newBody; }
I'm getting the exact same error for the same reason. $$anonymous$$y code below:
Transform modelGO = Instantiate(Resources.Load(dbEntry.path, typeof(Transform))) as Transform;
Debug.Log("instantiated model");
modelGO.parent = thisTransform;
Just tried that and I am getting the same error. Are you not able to repo this? I'm running Unity 5.1.1f1.
I'm wondering if it has something to do with loading from Resources in my case.
Found my problem! I was calling the Init() function (which I posted the code from above) on the prefab.
$$anonymous$$yClass myClass = Resources.Load(myClassPath, typeof($$anonymous$$yClass)) as $$anonymous$$yClass;
Debug.Log("Instantiating...");
Instantiate($$anonymous$$yClass);
$$anonymous$$yClass.Init(dbEntry);
Your answer
Follow this Question
Related Questions
setting the parent of a transform which resdes in a prefab is dusabled to prevent data corruption 1 Answer
Enemy only chasing pre-fab's original location, not instance's 2 Answers
Accessing children of instances vs children of original prefab 1 Answer
Howto set main.camera as a parent when main.camera itself is a child of a prefab 1 Answer
Instantiate a Prefab as child 0 Answers