- Home /
Parenting an instantiated Prefab?
I have a Spawner which I instantiate, and then try to parent with
gameObject.transform.parent = Sun.transform;
It doesn't complain about the code, but when I start the build it says that "setting the parent of a transform which resides in a prefab is disabled to prevent data corruption"
How can I solve this?
what game object instantiate your prefab ?
make your game object child of Sun !
then if you instantiate a prefab , its automatically be a child of sun :)
Qasem: have you tried that? I don't think prefabs get your parent. They only get the position and rotation you give when making them.
Answer by Kiwasi · Dec 31, 2014 at 07:46 PM
GameObject clone = (GameObject)Instantiate(myPrefab);
clone.transform.SetParent(someOtherTransform);
Yeah I think I am instantiating my objects wrong: Instantiate (Sun.transform, spawnPosSun, Quaternion.identity); So I guess I'll try as mormon said, or maybe defining the planet prefab as the sun prefab's child. I'll update when I know. Thank you all!
Answer by Owen-Reynolds · Dec 31, 2014 at 07:55 PM
What BoredM is saying (I assume) is that you are probably mistakenly setting the actual prefab to be a child. Make sure you set the Instantiated object instead.
So in the example, you might be setting myPrefab.parent
by mistake (that's what the error says.)