- Home /
Question by
RichCoggin · Aug 02, 2013 at 11:13 AM ·
instantiateparentchild
Instantiate object as child of object in scene c Sharp
Hi there,
I'm trying to work out how to instantiate an object as a child of another object in a scene. I've looked around the sites/forums etc and struggling to get the right code for it. I know how to instansiate and object into the scene, but I need it to be a child of an object. Here's the code line to instantiate the object for the scene:
if (selGridInt == 0)
{
GameObject objnewObject = (GameObject)Instantiate(newObject, new Vector3(0,10,0), transform.rotation);
}
The 'newObject' is a Prefab in a folder in the project and an empty game object called 'holder' is in the scene.
Any help with this would be fabulous! c Sharp.
Cheers,
Rich
Comment
Answer by nixcs2512 · Aug 02, 2013 at 12:12 PM
You can access parent and child of an object through its Transform, so it likes:
Transform parentObject;
void Start()
{
GameObject objnewObject = (GameObject)Instantiate(newObject,new Vector3(0,10,0),transform.rotation);
objnewObject.transform.parent = parentObject;
//or parentObject.transform if parentObject type is GameObject
}