Create GameObject with another gameObject's position

Hello,

I’m new to unity, and I’m trying to make something that looks like Space invaders. When the player presses the spacebar, I generate a cube, and then translate it updwards. What I’d like to do is, when I create the cube, which would be the bullet, create it on the same position as the player.

Here’s my code:

bullet = GameObject.CreatePrimitive(PrimitiveType.Cube);
bullet.AddComponent ('translateBullet');

and the translateBullet.js has:

function Start(){

var player = GameObject.FindWithTag('Player');
transform.position = player.transform.position;
transform.position = Vector3.up * 5;
transform.localScale.x = 20;
transform.localScale.y = 20;
transform.localScale.z = .1;
}

function Update () {

transform.Translate(Vector3.up * 1.5);
}

you’re overwriting the position of your transform.

you first set it to the player’s position but then set it to the up vector times 5 (completely blowing away it’s old position).

Try this:

transform.position = player.transform.position + ( Vector3.up * 5 );

OR

transform.position = player.transform.position;
transform.position += ( Vector3.up * 5 ); // note the += not just =