Parenting at runtime with precision, help!


I am having trouble getting one object parented to the right point. Basically I have two spheres, a blue one and a red one. The blue one has two small yellow cylinders on the either side of it (sort of like arms or airplane wings). The red sphere has one cylinder on it's front (like a nose). I am in control of the blue sphere and shoot little cyan squares at the red sphere. When I shoot it five times (it's health is at 0) it is no longer rendered and all that is visible is the halo effect. The gravity and detect collisions is also turned off at this point. Then the red sphere looks at me (the blue sphere) and translates forward. Then when it is close enough it snaps into the same position as an empty that is at the end of one of the cylinders and becomes parented to the blue sphere. In addition it has it's render restored and it appears solid again. The problem is that it does not parent so that the cylinders of each sphere match up forming a kind of link or bond together. Any ideas as to how I could solve this? I want the red sphere to eventually wander around with some AI but when shot 5 times will fly over and attach or connect to the blue sphere and then have them become a single unity. In addition it would be helpful if I could recalculate the center of the two combined objects so that the pair will rotate around the center of the two spheres. Here are some pictures.

starting up


Coming at me

This is how it connects faulty

A sloppy version of how it should look

    //This script finds the empties position
static var MPosition : Vector3;
static var ActiveMagnet : GameObject;

function Update () {


    //This finds the red sphere (enemy)'s position
static var EPosition : Vector3;

function Update () {



    //This finds the Distance between the blue and red spheres

static var TotalDistance : float;

function Update () {

    var EP=EnemyPosition.EPosition;

    var MP=MagnetPosition.MPosition;

    var DistanceApart=MP-EP;

    var DAX=Mathf.Abs(DistanceApart[0]);

    var DAY=Mathf.Abs(DistanceApart[1]);

    var DAZ=Mathf.Abs(DistanceApart[2]);

    TotalDistance=DAX + DAY + DAZ;


    //This is the script that handles the enemy's health and the magnetism and stuff
var Magnet : Transform;

var sprites= new Array();

var MagnetForce= 0.1;

var Enemy : Transform;

private var On : boolean= false;

private var EHealth=50;

function OnCollisionEnter (collision : Collision) {

    if(collision.gameObject.tag==("Player Bullet") && EHealth > 0){

        EHealth= EHealth - 10;




        gameObject.layer= 8;

        gameObject.rigidbody.detectCollisions = false;

        gameObject.rigidbody.useGravity = false;

        renderer.enabled= false;

    if(renderer.enabled != true){
        On= true;

function Update(){

    if(On != false){
        gameObject.transform.Translate(Vector3.forward * MagnetForce, Space.Self);

    if(On != false && DistanceApart.TotalDistance <= 1.5){
        Enemy.parent= Magnet;
        On = false;
        EHealth=EHealth + 10;
        renderer.enabled= true;

I'm sorry this is such a lengthy problem, but I greatly appreciate any help I can get. I would be more than happy to upload the source if someone alerts me to some method of doing so. Again thank you for all the help, the Unity community really is one of the best tools this engine has to offer! I might also run into a problem with having multiple enemies doing the same thing. I'll probably use the array, sprites, later on once I get this problem solved.

use transform.parent to make another object parent of this one. Then the children's movement and rotation and scale will be related to it's parent.