How to pass an object's position to a variable in a different script, and have it update?

In my game I have a player and an enemy, each with their own script. The enemy’s movement is controlled by rigidbody.MovePosition in FixedUpdate.

In my player script, I can have a variable which is updated with the players position as he moves, by doing this:

var playerPos : Vector3;

function Update()
{
  playerPos = transform.position;
}

This works just fine. However, if I try to set a variable in the same script to get the enemy’s position, like this:

var enemy : Transform;
var enemyPos : Vector3;

function Update()
{
  enemyPos = enemy.transform.position;
}

Then the variable is set with the enemy’s initial position, but it does not update as the enemy moves. Does anyone know why this might be? I’m at a loose end.

Thanks in advance for any help offered.

Your code works fine on my machine…
Did you put the correct Transform in the enemy?

var enemy : Transform;
var enemyPos : Vector3;

function Update()
{
  enemyPos = enemy.transform.position;
  Debug.Log(enemyPos);
}

What does the Debug.Log say?

I’ve worked out how to get it working. As I said above, when I was setting the transform of enemy in the player script, I had dragged and dropped the enemy prefab from the ‘Project’ list, and never applied it in the inspector, but when I dragged the enemy prefab from the hierarchy list in my scene into the transform for enemy in the player inspector and then pressed ‘apply’ it seemed to work. No idea why it’s done that but it’s working now