Enemy Spawning and Despawning Not Working Properly

I have a script on my player that is meant to spawn an enemy 400 units away from him every 10 seconds. The enemy in turn has a script that makes it despawn if it moves 800 units away from the player. However something isn’t working properly, and I can’t figure out what’s wrong.
The issue is that when the player moves too far away from his original location, the enemies start despawning instantly. Either this means that they end up spawning too far away, meaning that they actually don’t spawn around the player, or that they miscalculate the distance to the player after a while. Or something else. I have no idea.

This is the script on the player to spawn enemies:

#pragma strict

var testEnemy : GameObject;

InvokeRepeating("SpawnDelay", 10f, 10f);

function SpawnDelay ()
{
    var randomRotation = Quaternion.Euler(Random.Range(360, 10), 0, 0);
  
    Instantiate(testEnemy, transform.position + (transform.forward * 400), randomRotation);
    //testEnemy.transform.Translate(new Vector3(400f, 0, 0));
}

(The commented out line in the bottom is the other method I used for moving the enemy 400 units away from the player. Now I use “+ (transform.forward * 400)”, but neither seem to work. I’d prefer the commented out one though since the other one only spawns enemies in front of the player instead of at a random angle)

And here’s the script to make enemies despawn:

var Player : Transform;
var directionToTarget : Vector3;
var Angle : float;
var Distance : float;
var currentRotation : float;

function Update ()
{
    directionToTarget = transform.position - Player.position;
    Angle = Vector3.Angle(transform.forward, directionToTarget);
    Distance = directionToTarget.magnitude;
  
    if (Mathf.Abs(Angle) > 90 && Mathf.Abs(Angle) < 180 && Distance < 80)
    {
        //Debug.Log("I see you");
    }
  
    if (Distance > 800)
    {
        Destroy(gameObject);
        Debug.Log("Despawned");
    }
}

(There’s also a line of sight system in there, I jammed them together since they both use distance to the player)

Can someone please help me find the issue?

Have you tried putting a debug in when the enemy instantiates that shows its position & the player position? That would let you check if the enemy spawn point is moving with the player or if it is staying in a fixed spot after the first enemy is created.

How is that done? The idea has crossed my mind, but my knowledge about debug stops at the basic Debug.Log.

Just put in 2 debug.log lines. Make sure you use a string on each one to say what it is returning so that when you see it in the console you know which debug line belongs to each set of coords.

After testing a bit I have come to the conclusion that when the enemies are further than 800 units away from the spawnpoint, they despawn. So the Distance variable is apparently not centered on the player… But I don’t get why not.

Take out the *400 & see if it instantiates enemies 1unit in front no matter where you stand (you may want to deactivate any scripts on your enemies first so they don’t kill your player). That should let you easily see if they are spawning in the same spot over & over no matter where your player is. If they keep spawning in front of the player no matter where the player is then the issue is in the formula.

I would add a Debug.Log like:

directionToTarget = transform.position - Player.position;
Debug.Log(Player.position);

My first suspicion is that your Player.position vector is maybe not updating and is just sticking on where it’s set on the first call.

Late Edit:
Been a while since I’ve done any Unity code stuff, but if you haven’t fixed it yet, you may just need to do something like the following:

directionToTarget = new Vector3(transform.position - Player.position);

I figured it out. The problem wasn’t with the script, but the fact that the Player variable was set to the player prefab and not the player itself. Now it works fine.