Search Unity

How to attach two gameObjects together programmatically?

Discussion in 'Editor & General Support' started by aerende, Oct 3, 2010.

  1. aerende

    aerende

    Joined:
    Apr 27, 2009
    Posts:
    316
    I have two Game Objects that are moving. When their colliders make contact, as indicated by OnTriggerEnter, I stop changing the position of GameObject1, and make it a child of GameObject2:

    Code (csharp):
    1. GameObject1.transform.parent = GameObject2.transform;
    GameObject2 is still being moved using

    Code (csharp):
    1. GameObject2.transform.Translate(moveVector);
    But what happens is that both objects stop moving and Unity hangs. Can anyone tell me the correct way to attach two game objects and move one of the game objects and the other game object moves in the same manner as the first game object?

    [FIXED] - Bug in my code caused Unity to hang. The parent approach above works fine.
     
    Last edited: Oct 3, 2010
  2. Dylan_the_nipple

    Dylan_the_nipple

    Joined:
    Dec 24, 2015
    Posts:
    1
    could you paste the entire script please? :)