Object Not Dying When Approached

Hi! I’m trying to make an object die when the player approaches it. Any tips on what I’m doing wrong?

Script for the object (It patrols around)

using System.Collections;
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

public class NPCPatrol : MonoBehaviour
{
    //Determines whether NPC waits on a node
    [SerializeField]
    bool _patrolWaiting;

    //Sight distance
    [SerializeField]
    public float sightDistance = 10f;

    //Total time to wait on a node
    [SerializeField]
    float _totalWaitTime = 3f;

    //Probability to switch directions
    [SerializeField]
    float _switchProbability = 0.2f;

    //List of all patrol points to go to
    [SerializeField]
    List<PatrolPoint> _patrolPoints;

    //Private variables for behavior
    NavMeshAgent _navMeshAgent;
    int _currentPatrolIndex;
    bool _travelling;
    bool _waiting;
    bool _patrolForward;
    float _waitTimer;

    Transform target;
    Transform pad;
    // Start is called before the first frame update
    void Start()
    {
        target = PlayerTracker.instance.player.transform; //Calls on GameManager's PlayerTracker
        pad = PlayerTracker.instance.pad.transform;
        _navMeshAgent = this.GetComponent<NavMeshAgent>();

           if(_navMeshAgent == null) //If true no nav mesh attached
           {
               Debug.LogError("Nav mesh agent is not attached to " + gameObject.name);
           }
           else
           {
               if(_patrolPoints != null && _patrolPoints.Count >= 2) //Checks if patrol points exist and there are enough
               {
                   _currentPatrolIndex = 0;
                   SetDestination();
               }
               else
               {
                   Debug.Log("Insufficient patrol points for basic patrolling behaviour.");
               }
            }

    }

    // Update is called once per frame
    public void Update()
    {
        float distance = Vector3.Distance(target.position, transform.position);
        //Checks if close to the destination
        if (_travelling && _navMeshAgent.remainingDistance <= 1.0f)
        {
            _travelling = false;

            //If going to wait
            if (_patrolWaiting)
            {
                _waiting = true;
                _waitTimer = 0f;
            }
            else
            {
                ChangePatrolPoint();
                SetDestination();
            }
        }
        //Instead if we're waiting
        if (_waiting)
        {
            _waitTimer += Time.deltaTime;
            if(_waitTimer >= _totalWaitTime)
            {
                _waiting = false;

                ChangePatrolPoint();
                SetDestination();
            }
        }
        if (distance <= sightDistance)
        {
            gameObject.SetActive(false);
        }
    }

    private void SetDestination() //Sets destination
    {
        if(_patrolPoints != null)
        {
            Vector3 targetVector = _patrolPoints[_currentPatrolIndex].transform.position; //Go to destination
            _navMeshAgent.SetDestination(targetVector);
            _travelling = true;
        }
    }

    private void ChangePatrolPoint()
    {
        if(UnityEngine.Random.Range(0f, 1f) <= _switchProbability)
        {
            _patrolForward = !_patrolForward;
        }

        if (_patrolForward)
        {
            _currentPatrolIndex = (_currentPatrolIndex + 1) % _patrolPoints.Count;
        }

        else
        {
            if(--_currentPatrolIndex < 0)
            {
                _currentPatrolIndex = _patrolPoints.Count - 1;
            }
        }
    }
    void OnDrawGizmosSelected()
    {
        Gizmos.color = Color.blue; //Makes sight radius sphere blue
        Gizmos.DrawWireSphere(transform.position, sightDistance); //shows radius of sight
    }
}

Script for the GameManager that tracks the player

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

public class PlayerTracker : MonoBehaviour
{
     #region Singleton

    public static PlayerTracker instance;

    void Awake()
    {
        instance = this;

    }

    #endregion

    public GameObject player; //References player
    public GameObject pad;
}

If you need anything else to help solve the problem let me know

Multiple ways to approach this. One is to use a trigger around the object and check upon OnTriggerEnter from the player and then destroy the object. I recommend this variant.

Consider the following code placed on your player, and having your NPC having a collider with isTrigger enabled, and perhaps tagged with tag NPC:

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

public class NPCDestroyer : MonoBehaviour {
    void OnTriggerEnter(Collider other) {
        if (other.CompareTag("NPC")) {
            Destroy(other.gameObject);
        }
    }
}

Alternative 2 is doing it via script. This can be done simply by checking the distance between the two positions of player and the NPC object, much like you’re already doing in your patrol script for deciding when to start moving to the next patrol point.

Consider the following script placed on the NPC game object:

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

public class PlayerTracker : MonoBehaviour {
    public Transform player;
    public float range = 10;

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

        if (distance < range) {
            Destroy(gameObject);
        }
    }
}