Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

can you add to other object as child?

Discussion in 'Scripting' started by depth, Sep 4, 2012.

  1. depth

    depth

    Joined:
    May 3, 2012
    Posts:
    4
    Ok, ive had a look though the scripting reference, and i cant see anything that might allow me to add gameobjects to other gameobjects as children through the use of C#, only removing all children. can anyone help as this would make my life alot eaiser...

    if you need context then it's for an RTS, i want to keep units in formation while moving so figured if i create a new object, add add the selected units to it then just move that object, also would work for control groups.
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Use transform.parent.

    --Eric
     
  3. depth

    depth

    Joined:
    May 3, 2012
    Posts:
    4
    not sure how, there are no functions to add an object to another as a child. nothing obvious anyway, could you give me an example please?

    im trying to set it out that when i select 4 units in a (for example) line at points '1,1' , '1,2' , '1,3' and '1,5' they will all be added to a new gameobject and then moving will just be moving that new gameobject to the required place and the units would follow (say if i move the new gameobject by '5,2' and rotate by 90deg all the other objects would move to the right places without having to have them all move under there own scripts.)
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    As I said, transform.parent. Please just look it up in the docs, it will take you 2 seconds.

    --Eric
     
  5. depth

    depth

    Joined:
    May 3, 2012
    Posts:
    4
    Oh god i cant read :( so sorry, thank you, i looked through it and thought there would be a lot more to it than that so dismissed it originally. thankyou again :D
     
  6. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Unity often has that problem..."Nah, that's too simple, it wouldn't work...oh wait." ;)

    --Eric