how to track postition of a moving object?

hi guys,

i am a noob, looking for advise. i instantiate a moving enemy and want to track it’s changing transform. Unfortunately i only get the transform from start although i see the enemy moving on the screen.

Instantiating my enemy:

public var enemy : GameObject;

function Start () {

Instantiate(enemy, Vector3(5.886453,0.4570506, 4.031569), Quaternion.identity);

}

function Update () {

Debug.Log(enemy.transform.position);

}

Script attached to my enemy-prefab:

var speed : float = 5;

function Update () {

transform.Translate(Vector3(speed,0,0) * Time.deltaTime);

}

when i press play, the console keeps printing 0,0,0 although my enemy prefab is moving.

how can i get the updated real transform of the enemy?

thanks!

Your “enemy” variable is a reference to the enemy prefab, but you’re moving an enemy instance.

Instantiate returns a pointer to the instance created, so store that reference and debug.log it’s position:

 public var enemy : GameObject; //prefab
 private var instance : GameObject;

 function Start () {
 
 instance = Instantiate(enemy, Vector3(5.886453,0.4570506, 4.031569), Quaternion.identity);
 
 }
 
 function Update () {
 
 Debug.Log(instance.transform.position);
 
 }

Try this:

public var enemyModel : GameObject; //The reference
public var enemy : GameObject; //The actual enemy

function Start(){
 enemy = Instantiate(enemyModel, Vector3(5.886453,0.4570506, 4.031569), Quaternion.identity);
}
 
function Update(){
 
Debug.Log(enemy.transform.position);
 
}