How can I parent a GameObject to and instance of another GameObject?
I have a game object sitting in the level I am currently creating and when the player gets near it I instantiate a prefab at the same location. I want to have the original GameObject attach to the instance so I do this:
orginalGameObject.Transform.Parent = instantiatedGameObject.Transform
but I get an error telling me that parenting to an instance is not allowed to prevent data corruption.
Is there a way I can do this? I want the original GameObject to be attached to that it can be dragged away by it. The instantiated GameObject is also a RigidBody which is jointed using a spring joint, just in case this should make any difference.
Thanks for the help.
Answer by Raven_Gothchild · Sep 20, 2016 at 12:46 PM
OK, so when I went back to this bit of code I found that I was not referencing the instance of the prefab but the prefab itself so a change in the code to instantiate the object to a GameObject allowed me to reference the instance and parent the original game object to this instance.
InstanceGameObject = Instantiate (Prefab, Vector3Position, Quaternion.identity) as GameObject;
SceneGameObject.transform.parent = InstanceGameObject.transform;
Works without error.
Your answer
![](https://koobas.hobune.stream/wayback/20220612092221im_/https://answers.unity.com/themes/thub/images/avi.jpg)