My enemies started moving towards player automatically after I added a score tracker.

Im a bit stumped here, I just added a score tracker to my game and before I did everything was working normally. My enemies did not start moving until my player got close enough, now as soon as I Implemented the score tracker they all just started moving as soon as the game starts and the score doesnt add up after the first time of doing so. I included a screenshot with my Unity settings for enemy as well as the code. Any help would be great Thanks in advance!

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Enemy : MonoBehaviour
{
    [Header ("Stats")]
    public float walkSpeed;
    public float stoppingDistance;
    public float nearDistance;
    public float startTimeBtwShots;
    public float shootSpeed;
    private float timeBtwShots;
    public Transform firePoint2;
    private Vector2 direction;
    bool facingRight = false;
    bool canShootLeft = true;
    public Animator animator;
    public float dieTime;
    public int Score;
    public Text ScoreText;

  
  

    [Header ("References")]
  
    private Transform player;
    private Transform enemy;
    private float dist;
  
  

    public int health = 100;

    public GameObject deathEffect;
    public GameObject bullet;

    public void TakeDamage (int damage)
    {
        health -= damage;

        if (health <= 0)
        {
            Die();
            animator.Play("EnemyDeath");
        }
        animator.SetTrigger("Hurt");
        animator.Play("EnemyHurt");
    }
    void Start ()
    {
        player = GameObject.FindGameObjectWithTag("Player").transform;
        timeBtwShots = startTimeBtwShots;
    }

    void Update ()
    {
        dist = Vector3.Distance(player.position, transform.position);

        if(dist <= nearDistance)
        {
            Shoot();
        }

      
  
        //Makes the enemy move
        if(Vector2.Distance(transform.position, player.position) > nearDistance){
            transform.position = Vector2.MoveTowards(transform.position, player.position, walkSpeed * Time.deltaTime);
        } else if(Vector2.Distance(transform.position, player.position) > stoppingDistance){
            transform.position = Vector2.MoveTowards(transform.position, player.position, walkSpeed * Time.deltaTime);
        } else if(Vector2.Distance(transform.position, player.position) < stoppingDistance && Vector2.Distance(transform.position, player.position) > nearDistance){
            transform.position = this.transform.position;
        }
      
      

        if(player.position.x > transform.position.x && transform.localScale.x > 0
         || player.position.x < transform.position.x && transform.localScale.x < 0)
        {
         Flip();
        }

              
        if(player.position.x < transform.position.x)
        {
            facingRight = false;
            canShootLeft = true;
        }

      

    }

    void Shoot()
    {
        //Makes the enemy shoot
     if(canShootLeft && timeBtwShots <= 0){
     GameObject newBullet = Instantiate(bullet, firePoint2.position, Quaternion.identity);
     newBullet.GetComponent<Rigidbody2D>().velocity = new Vector2(shootSpeed * -walkSpeed * Time.fixedDeltaTime, 0f);
     timeBtwShots = startTimeBtwShots;
     } else {
     timeBtwShots -= Time.deltaTime;
     }
        //Makes the enemy shoot when facing right.
     if(facingRight){
        canShootLeft = false;
     if(facingRight && timeBtwShots <= 0){
     GameObject newBullet = Instantiate(bullet, firePoint2.position, Quaternion.identity);
     newBullet.GetComponent<Rigidbody2D>().velocity = new Vector2(shootSpeed * walkSpeed * Time.fixedDeltaTime, 0f);
     timeBtwShots = startTimeBtwShots;
    
     } else {
     timeBtwShots -= Time.deltaTime;
     }
     }
  
    }
    //Flips sprite image direction.
    void Flip()
    {
       facingRight = true;
       canShootLeft = false;
      
       transform.localScale = new Vector2(transform.localScale.x * -1, transform.localScale.y);
  
       walkSpeed *= 1;
       if(Vector2.Distance(transform.position, player.position) > nearDistance){
       transform.position = Vector2.MoveTowards(transform.position, player.position, walkSpeed * Time.deltaTime);
     } else if(Vector2.Distance(transform.position, player.position) > stoppingDistance){
       transform.position = Vector2.MoveTowards(transform.position, player.position, walkSpeed * Time.deltaTime);
     } else if(Vector2.Distance(transform.position, player.position) < stoppingDistance && Vector2.Distance(transform.position, player.position) > nearDistance){
       transform.position = this.transform.position;
     }

      
    }

  
      IEnumerator CountDownTimer()
    {
       yield return new WaitForSeconds(dieTime);
       Died();
    }
  
    void AddScore()
    {
        Score += 10;
        ScoreText.text = Score.ToString();
    }
  

    void Die ()
    {
        animator.SetBool("IsDead", true);
      

       GetComponent<Collider2D>().enabled = false;
       this.enabled = false;
       StartCoroutine(CountDownTimer());
    }

    void Died ()
    {
        Destroy(gameObject);
        AddScore();

    }



}

I dont see a screenshot included fyi. Do you have any errors showing in the console? What happens if you turn off your AddScore() function? We need a bit more info to really help you.

Start throwing in some Debug.Logs to functions. See what is calling, what isn’t, etc.

thats my bad the file never ended up loading. I ended up solving it by removing the score tracker that was attached the to Player and made it its own element then I re worked the enemy movement script to function more optimally. Thanks for reaching out!