I have a bullet that has to hit a constantly moving enemy.

So, in BulletScript, I have declared a Transform

public Transform enemy;               //and assigned enemy object to it that is continuously moving and changing its position

Now, when I try to use enemy.position in bullet script so as to hit it, enemy.positiongives the position at which the enemy started and not the position at which it was when bulletprefab was shot.

How can I get the updated position of enemy object every time bulletprefab is instantiated.

  • \$\begingroup\$ Your comment says the object is contantly changing its position, but the text after it suggests its position stays the same. Which is it? \$\endgroup\$
    – Anko
    Jul 27, 2014 at 12:40
  • \$\begingroup\$ @Anko: enemy object is constantly moving and I am trying to get its updated position in the bullet script, and so created Transform called Target and assigned enemy object to Target, but when I try to use Target.position, I get the position at which the enemy(Target) starts to move from and not the position at which it currently is \$\endgroup\$
    – systemdebt
    Jul 27, 2014 at 12:44

1 Answer 1


How are you retrieving the value of enemy? It is assigned at compile time, or retrieved at runtime?

My advice would be to declare it as a GameObject, assign its value at runtime and then access its Transform value.

  • \$\begingroup\$ Could you please give an example: Say, instead of public Transform, I declared, Public GameObject , then? How do I assign the GameObject at runtime? \$\endgroup\$
    – systemdebt
    Jul 27, 2014 at 13:30
  • \$\begingroup\$ I think, what you just mentioned is the problem. because, The position to it is being assigned at compile time and retrieved at runtime, but I am not clear that how do I change that, please? \$\endgroup\$
    – systemdebt
    Jul 27, 2014 at 13:34
  • 1
    \$\begingroup\$ Well, the answer is: It depends on your game logic! How is the target assigned? If there is only one target and it will never change, you can assign a tag or name to that object at compile time and retrieve it at runtime. If there are several of them then you should define your game logic that decides which object is retrieved. Ultimately what you need to do depends on what you want to do! If you want a more concrete answer, please provide more details. \$\endgroup\$
    – angarg12
    Jul 28, 2014 at 6:29
  • \$\begingroup\$ Yes, it's just one object translating and changing its position, I assigned the object in the Inspector Panel \$\endgroup\$
    – systemdebt
    Jul 29, 2014 at 15:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .