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. Dismiss Notice

adding childgameobject

Discussion in 'Scripting' started by verderveremem, Jul 1, 2014.

  1. verderveremem

    verderveremem

    Joined:
    May 11, 2014
    Posts:
    52
    hi,
    how can i add childgameobject to the main gameobject in the script?
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,738
    Code (csharp):
    1.  
    2. GameObject newObject = new GameObject("Child"); //you can use use "Instantiate" here
    3. newObject.transform.parent = transform;
    4.  
     
    verderveremem and Magiichan like this.
  3. verderveremem

    verderveremem

    Joined:
    May 11, 2014
    Posts:
    52
    hi,
    i dont want to add a new childobject to the maingameobject. i want to add the childobject which is already existed to the main gameobject. And also can you help me for that how can i remove this childobject from the main object?

    thanks a lot
     
  4. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    I don't understand what "Add the child object which is already existed to the main gameobject" means.

    Do you mean it is in the scene, but not a child yet?

    Code (csharp):
    1.  
    2. GameObject newObject = GameObject.Find ("ObjectName");
    3. newObject.transform.parent = transform;
    4.  
    To unparent
    Code (csharp):
    1.  
    2. Transform newObject = transform.Find ("ObjectName");
    3. newObject.parent = null;
    4.  
     
    verderveremem likes this.
  5. verderveremem

    verderveremem

    Joined:
    May 11, 2014
    Posts:
    52

    i solved the problem what i asked to you. Thanks a lot