Enemy not moving

I have an enemy object and a player object. The enemy is supposed to follow the player object. However, it does not do so. Instead, the enemy moves in a fixed direction forward, no matter the position of the player.

private var controller : CharacterController;
var speed : float; 
var target : GameObject;

function Start () {
	controller = GetComponent(CharacterController);

}

function Update () {

	if (transform.position.x < target.transform.position.x)
		{
		controller.Move(Vector3.right * speed * Time.deltaTime);
		}
	else if (transform.position.x > target.transform.position.x)
		{
		controller.Move(Vector3.left * speed * Time.deltaTime);
		}
	else if (transform.position.z < target.transform.position.z)
		{
		controller.Move(Vector3.forward * speed * Time.deltaTime);
		}
	else if (transform.position.z > target.transform.position.z)
		{
		controller.Move(Vector3.forward * -speed * Time.deltaTime);
		}
}

What is wrong here!?

Why not put z and x in the same line? Meaning…

if (x > target.x  z > target.z)
{}

the way you have it, z will always be more or less, so that line will always get called and be the dominant one since it is last. Meaning no matter what, it is called.

Your code will only move the enemy in the X axis since your movement code in the Z direction is “blocked” by the ELSE statements.
If you change

else if (transform.position.z < target.transform.position.z)  {
        controller.Move(Vector3.forward * speed * Time.deltaTime);
        }

to

        if (transform.position.z < target.transform.position.z) {
        controller.Move(Vector3.forward * speed * Time.deltaTime);
        }

the Backward/forward movment code will also get executed.

But this will make movement jittery since you not actually taking into account the times when the enemy is on the same X or Z axis, it’s either smaller or larger and will therefore jitter back and forth.

There is also a very much easier way of doing this with some simple vector math.
If you take your target position and subtract your current position it will give you a vector with the direction you need to travel, just normalize this vector and use it to move with.

void Update() {
      Vector3 direction = target.transform.position - transform.position;
      direction = direction.normalized
      controller.Move(direction * speed * Time.deltaTime);
}

Much simpler and cleaner.