Search Unity

How to instantiate a game object into others GameObject?

Discussion in 'Scripting' started by lucked, Sep 6, 2011.

  1. lucked

    lucked

    Joined:
    Jul 11, 2010
    Posts:
    111
    How can i instantiate a game object into others GameObject?
     
  2. PrimeDerektive

    PrimeDerektive

    Joined:
    Dec 13, 2009
    Posts:
    3,090
    What do you mean?
     
  3. lucked

    lucked

    Joined:
    Jul 11, 2010
    Posts:
    111
    example i has one game object with 4 cubes i want put more cubes intro this gameobject
     
  4. lucked

    lucked

    Joined:
    Jul 11, 2010
    Posts:
    111
    its possible do this!
     
  5. lucked

    lucked

    Joined:
    Jul 11, 2010
    Posts:
    111
  6. PrimeDerektive

    PrimeDerektive

    Joined:
    Dec 13, 2009
    Posts:
    3,090
    do you mean parented? one GameObject cannot be more than one GameObject, that doesn't make sense. You can parent GameObjects to each other though (or more accurately, parent their transforms). That's what happens when you drag them on top of each other in the scene pane.

    Once you instantiate your GameObject just set the parent:

    Code (csharp):
    1.  
    2. var instantiatedObject : GameObject = Instantiate(myPrefab, pos, rot);
    3. instantiatedObject.transform.parent = parentObject.transform;
    4.  
     
  7. lucked

    lucked

    Joined:
    Jul 11, 2010
    Posts:
    111
    i will try
     
  8. bdev

    bdev

    Joined:
    Jan 4, 2011
    Posts:
    656
    Yep thats how you do it. most likely you'll want to immediately after setting the parent you'll want to set stuff like

    transform.localPosition = Vector3.zero;
    transform.localRotation = Quaternion.identity;
    transform.localScale = Vector3.one;

    unless when you instantiate with pos and rot you already set it to use a position you want. but keep in mind if your using scaling througout your project you'll still want to set localscale manually.