# Why does the script stop working after respawning?

I have a flying enemy that follows the player:

public class MoveTowardsPlayer : MonoBehaviour
{
public float moveSpeed, sightDistance;
public Rigidbody2D theRB;
public Transform target;
private void Start()
{
theRB = GetComponent<Rigidbody2D>();
}
private void Update()
{
Vector3 targetDir = target.position - transform.position;
targetDir = targetDir.normalized;
float dist = Vector3.Distance(target.position, transform.position);
if (dist < sightDistance)
theRB.velocity = targetDir * moveSpeed;
}

}


And here is what happens when the enemy dies, and then respawns:

  public void Die()
{
Instantiate(enemyDieEffect, transform.position, transform.rotation);
gameObject.SetActive(false);
}

public void Respawn()
{
gameObject.transform.position = initialPosition;
gameObject.SetActive(true);
currentHealth = maxHealth;
}


The enemy respawns if the player dies and goes back to the last checkpoint.

Interestingly, if the player dies before reaching any checkpoint, the MoveTowardsPlayer scripts still works after respawning. It only stops working after any of the checkpoints are reached.

For the sake of completeness, here is the Checkpoint script, which has absolutely nothing to do with the enemies:

public class Checkpoint : MonoBehaviour
{
private Animator animator;
public bool isReached;

// Start is called before the first frame update
void Start()
{
isReached = false;
animator = GetComponent<Animator>();
}

private void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("Player"))
{
CheckpointController.instance.DeactivateCheckpoints();
animator.SetBool("isActive", true);
CheckpointController.instance.SetSpawnPoint(transform.position);
}
}

public void ResetCheckpoint()
{
animator.SetBool("isActive", false);
}
}

• Do you get any particular error message in the console? If not, what other symptoms of "not working" do you observe? Jul 2 '20 at 23:24
• Occasionally, I get an IndexOutOfBounds error, but not always. I assume that it is because of sync issues. I do not observe any other "not working" symptoms. The enemy still functions properly except the following part. Jul 2 '20 at 23:35

Interestingly, if the player dies before reaching any checkpoint, the MoveTowardsPlayer scripts still works after respawning. It only stops working after any of the checkpoints are reached.

Presumably your problem code is this:

if (dist < sightDistance) theRB.velocity = targetDir * moveSpeed;


When you respawn the enemy, you move it back to its original position. When you respawn the player after they reach a checkpoint, they are moved back to the checkpoint. I'm guessing the enemy spawnpoint is too far from the checkpoint and enemy can't "see" the player any more.

As an aside, it is very bad practice to put public variables in your classes. It's much safer/cleaner/easier to debug when you use private/protected variables and access them from other classes using properties. If you need the fields to appear in the inspector, you can use the [SerializeField] attribute:

//bad
public float moveSpeed;

//good
[SerializeField] private float moveSpeed;
public float MoveSpeed { get => moveSpeed; set => moveSpeed = value; }

• It cannot be the issue because I move the player close enough after respawning. Jul 2 '20 at 23:43
• Add this line of code to the Update() function: print("dist < sightDistance: " + (dist < sightDistance) + ", moveSpeed: " + moveSpeed + ", target: " + target);. It will spam messages in the console. Continue until the player respawns from a checkpoint, and see what the messages say. Jul 2 '20 at 23:50
• I did the Debugging, and to my surprise, the enemy was spawning at a different Z coordinate because of its parent object. Now I have set the parent's Z coordinate to zero, and everything works just fine. Jul 2 '20 at 23:52
• @padawan Great, glad you figured it out. Logging is often a good first step in diagnosing a feature that doesn't work. If this solved your issue, please don't forget to mark it as the correct answer! Jul 2 '20 at 23:54