Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Join us on Dec 8, 2022, between 7 am & 7 pm EST, in the DOTS Dev Blitz Day 2022 - Q&A forum, Discord, and Unity3D Subreddit to learn more about DOTS directly from the Unity Developers.
    Dismiss Notice
  3. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

instantiate object inside of another gameObject?

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

  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;
     
    vost, lolokiu1, khanhdang-nkn and 3 others like this.
  3. Cameron_SM

    Cameron_SM

    Joined:
    Jun 1, 2009
    Posts:
    914
    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:
    25
    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.
     
    aniruddhgbhayani likes this.
  7. Xrayer

    Xrayer

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