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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

[SOLVED]Instantiate as child?

Discussion in 'Scripting' started by conair360, Sep 19, 2009.

  1. conair360

    conair360

    Joined:
    Nov 15, 2008
    Posts:
    198
    How would I instantiate a RigidBody as a child of the object instantiating it?

    I have this attached to my character:
    Code (csharp):
    1. if (PlayerPrefs.GetString("Weapon") == "3"){
    2.         if (PlayerPrefs.GetString("Fire") == "true"){
    3.             var clone1 : Rigidbody;
    4.             clone1 = Instantiate(SniperCamera, transform.position, transform.rotation);
    5.             PlayerPrefs.SetString("Fire", "false");
    6.         }
    7.     }
    But this just makes a camera. How do I make the camera a child of the object that instantiated it?
     
    Helladah and RomeoNahon like this.
  2. flaminghairball

    flaminghairball

    Joined:
    Jun 12, 2008
    Posts:
    868
    Code (csharp):
    1. clone1.transform.parent = transform;
    :wink:
     
  3. conair360

    conair360

    Joined:
    Nov 15, 2008
    Posts:
    198
    Thanks, but I'm still having a little trouble with that. No matter where I put that in my code, it comes back with "NullReferenceException: Object reference not set to an instance of an object"

    I've looked through the documentation and they seem to have a limited page for transform.parent

    Could you show me where to put it in my script?
     
  4. Tempest

    Tempest

    Joined:
    Dec 10, 2008
    Posts:
    1,286
    When you put it directly after the Instantiate line, it gives you that error?
     
  5. conair360

    conair360

    Joined:
    Nov 15, 2008
    Posts:
    198
    That did it! Thanks!
     
  6. LocoAbreu

    LocoAbreu

    Joined:
    Feb 24, 2011
    Posts:
    1

    hey man, i put it directly after the instantiate line and NullReferenceException

    Code (csharp):
    1.     clone1 = Instantiate(CameraPrefab, transform.position, transform.rotation);
    2.     clone1.transform.parent = GameObject.Find("Player(clone)").transform;
    3.  
     
    Last edited: Feb 24, 2011
    shogoun94, hukudam and Lolwhatdafish like this.
  7. Krodil

    Krodil

    Joined:
    Jun 30, 2010
    Posts:
    141
    But it does not work with a prefab. Any way to bypass this?
     
  8. FriesB

    FriesB

    Joined:
    Nov 10, 2011
    Posts:
    1
    I don't think you can directly access a instantiated prefab, but i found a workaround:

    Code:
    Code (csharp):
    1.  
    2. public GameObject PrefabObj;
    3. private GameObject _InstanceObj;
    4.  
    5. //...
    6. int instanceID = GameObject.Instantiate(Prefab,position,rotation).GetInstanceID();
    7. GameObject[] instancesArr = GameObject.FindGameObjectsWithTag(PrefabObj.tag);
    8.  
    9. for(int i = 0; i < instancesArr.Length ; ++i)
    10. {
    11.     if(instancesArr[i].GetInstanceID() == instanceID)
    12.     {
    13.         _instanceObj = instancesArr[i];
    14.     }
    15.  
    16. }
    17.  
    18.  
    it may seam like it's not the fastest way to do it, but it's the only way that seams to work for me.

    oh, and sorry this is C# code, not javascript. instead of using the typenames in front of the object, use "var.... : GameObject[]" or something like that :) (i'm not good at js)
     
    Last edited: Nov 10, 2011
  9. jed1josh

    jed1josh

    Joined:
    Dec 17, 2011
    Posts:
    1
    This works well :)

    Thanks.
     
  10. boaheck

    boaheck

    Joined:
    Jan 25, 2014
    Posts:
    4
    Why not have a script on you're prefab like this?
    Code (csharp):
    1.  
    2. var player : Transform;
    3. function Start () {
    4. player = (GameObject.Find("player")).transform;
    5. transform.parent = player;
    6.  
     
    sadzanenyama likes this.
  11. zero3growlithe

    zero3growlithe

    Joined:
    Jul 7, 2012
    Posts:
    15
    UnityScript

    var t : Transform = Instantiate (somePrefab, transform.position, transform.rotation).transform;
    t.parent = transform;

    C#

    Transform t = ((GameObject)Instantiate (somePrefab, transform.position, transform.rotation)).transform;
    t.parent = transform;
     
  12. A.Killingbeck

    A.Killingbeck

    Joined:
    Feb 21, 2014
    Posts:
    483

    http://docs.unity3d.com/ScriptReference/Object.Instantiate.html

    As you can see it returns an Object. So naturally, you can access it directly.

    Transform t = Instantiate(prefab) as Transform;
     
  13. rbosse

    rbosse

    Joined:
    May 21, 2015
    Posts:
    18
    I would do it this way, just FYI.

    Code (CSharp):
    1. clone1.transform.parent = this.transform;
     
    Felix_M likes this.
  14. Felix_M

    Felix_M

    Joined:
    Feb 28, 2016
    Posts:
    11
    THANK YOU!
     
  15. Austin-Gregory

    Austin-Gregory

    Joined:
    Sep 19, 2013
    Posts:
    78
    Also, you have SetParent. Not that this isn't relevant here, but it is best practice to always use SetParent when working with UI elements since it gives you the option to keep the local orientation.

    I typically use SetParent for any object I'm changing the parent on.
     
    BRL0 likes this.
  16. juliuslaak

    juliuslaak

    Joined:
    Jan 27, 2016
    Posts:
    2
    That's exactly what I was looking for, thanks! Without the SetParent functionality, if you set your new clone position e.g. (3,3,3), after assigning new parent with position (0,0,0), the position of the clone would become (-3, -3, -3).