Search Unity

instantiate object inside of another gameObject?

Discussion in 'Scripting' started by kodagames, Dec 16, 2011.

Thread Status:
Not open for further replies.
  1. kodagames

    kodagames

    Joined:
    Jul 8, 2009
    Posts:
    548
    How is everyone this fine morning?

    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.
     

    Attached Files:

  2. andorov

    andorov

    Joined:
    Feb 10, 2011
    Posts:
    1,061
    var newObj = GameObject.Instantiate(somePrefabReference);
    newObj.transform.parent = GameObject.Find("EmptyGameObject").transform;
     
    MichaelGotlib, vost, lolokiu1 and 4 others like this.
  3. Cameron_SM

    Cameron_SM

    Joined:
    Jun 1, 2009
    Posts:
    915
    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:

    Code (csharp):
    1. // Makes the camera follow this object by
    2. // making it a child of this transform.
    3.  
    4. // Get the transform of the camera
    5. var cameraTransform = Camera.main.transform;    
    6.  
    7. // make it a child of the current object
    8. cameraTransform.parent = transform;
    9.  
    10. // place it behind the current object
    11. cameraTransform.localPosition = -Vector3.forward * 5;
    12.  
    13. // make it point towards the object
    14. cameraTransform.LookAt(transform);
    If you want to move an object back to the root of the scene hierarchy, set it's parent to null.

    Unity Manual (Transform.parent): http://unity3d.com/support/documentation/ScriptReference/Transform-parent.html
     
    ailuropoda0 likes this.
  4. kodagames

    kodagames

    Joined:
    Jul 8, 2009
    Posts:
    548
    Ahhh Sweeet!

    Thank you andorov greatly appreciated!!!
     
  5. SkylinR

    SkylinR

    Joined:
    Sep 4, 2015
    Posts:
    6
    I know that it's old question but for people which will look for this in the future.
    Now You should do it like that:

    Code (CSharp):
    1. Transform Parent;
    2. GameObject PrefabGameObject;
    3.  
    4. GameObject InstantiatedGameObject= Instantiate(PrefabGameObject);
    5. InstantiatedGameObject.transform.SetParent(Parent);
    More about it here https://docs.unity3d.com/ScriptReference/Transform.SetParent.html

    Or just use second parameter in Instantiate function like this

    Code (CSharp):
    1. Transform Parent;
    2. GameObject PrefabGameObject;
    3.  
    4. Instantiate(PrefabGameObject,Parent);
    More here https://docs.unity3d.com/ScriptReference/Object.Instantiate.html
     
    Last edited: Dec 14, 2018
  6. M_R_M

    M_R_M

    Joined:
    Jan 31, 2019
    Posts:
    26
    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:

    Code (CSharp):
    1. Instantiate(instantiatedObject, Vector3 position, Quaternion rotation, parentGameObject);
    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.
     
  7. Xrayer

    Xrayer

    Joined:
    Jun 28, 2018
    Posts:
    2
    thanks mate!, 8 years later
     
    vost and rommzesdt like this.
  8. gabi8maio

    gabi8maio

    Joined:
    Jul 14, 2023
    Posts:
    1
    Thanks guys! 12 years later. BTW I'm in the time where Unity put the fee installation.
     
  9. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,502
    That's enough pointless necroing. Closing the thread.
     
    Bunny83 likes this.
Thread Status:
Not open for further replies.