Is it possible to Instantiate a gameObject inside of another gameObject in the hierarchy?
if so how would I attempt it?
from the attached image I need to instantiate a gameObject inside of the EmptyGameObject where the other cubes are.
I know how to instantiate a gameObject in the scene but never had to do it inside of another gameObject before.
Thanks a million for any thoughts on how to do this.
Just set your newly instantiated object’s transform.parent field to the transform of the object want to, well, parent it to.
Example code from the manual:
// Makes the camera follow this object by
// making it a child of this transform.
// Get the transform of the camera
var cameraTransform = Camera.main.transform;
// make it a child of the current object
cameraTransform.parent = transform;
// place it behind the current object
cameraTransform.localPosition = -Vector3.forward * 5;
// make it point towards the object
cameraTransform.LookAt(transform);
If you want to move an object back to the root of the scene hierarchy, set it’s parent to null.
I know this is an ancient thread, but in case anyone runs into this issue and none of the suggestions on various forums work, the following worked for me:
And I just made the parent game object a public variable. Don’t know if it’s a version mismatch or what, but the Unity documentation on the overloads for this function were incorrect for me, stating that the parent needs to be set BEFORE position and after the instantiated object, whereas it actually needed to be set after rotation.