MissingReferenceException: The object of type 'Transform'

MissingReferenceException: The object of type ‘Transform’ has been destroyed but you are still trying to access it.
Your script should either check if it is null or you should not destroy the object.
UnityEngine.Transform.get_position () (at :0)
Enemy.Pursueplayer () (at Assets/Scripts/Enemy.cs:47)
Enemy.Update () (at Assets/Scripts/Enemy.cs:41)

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

public class Enemy : MonoBehaviour
    [Header("Enemy Health and Damage")]
    public float giveDamage = 5f;
    public float enemySpeed;

    [Header("Enemy Things")]
    public NavMeshAgent enemyAgent;
    public Transform LookPoint;
    public GameObject ShootingRaycastArea;
    public Transform playerBody;
    public LayerMask PlayerLayer;

    [Header("Enemy Shooting Var")]
    public float timebtwshoot;
    bool previouslyShoot;

    [Header("Enemy States")]
    public float visionRadius;
    public float shootingRadius;
    public bool playerInvisionRadius;
    public bool playerInshootingRadius;
    public bool isPlayer = false;

    private void Awake()
        enemyAgent = GetComponent<NavMeshAgent>();

    private void Update()
        playerInshootingRadius = Physics.CheckSphere(transform.position, visionRadius, PlayerLayer);
        playerInshootingRadius = Physics.CheckSphere(transform.position, shootingRadius, PlayerLayer);

        if(playerInvisionRadius && !playerInshootingRadius) Pursueplayer();
        if(playerInshootingRadius && playerInshootingRadius) ShootPlayer();

    private void Pursueplayer()

    private void ShootPlayer()


        if (!previouslyShoot)
            RaycastHit hit;

            if (Physics.Raycast(ShootingRaycastArea.transform.position, ShootingRaycastArea.transform.forward, out hit, shootingRadius))
                Debug.Log("Shooting" + hit.transform.name);

                PlayerScript playerBody = hit.transform.GetComponent<PlayerScript>();

                if(playerBody != null)


            previouslyShoot = true;
            Invoke(nameof(ActiveShooting), timebtwshoot);

     private void ActiveShooting()
        previouslyShoot = false;


The error message is indicating that the script is trying to access the “position” property of a “Transform” object that has been destroyed. This is likely happening in the “PursuePlayer()” and “ShootPlayer()” methods, where the script is trying to access the “playerBody” transform and “LookPoint” transform, respectively.
It seems like you are trying to access the playerBody’s position and look at LookPoint but both of these objects may be destroyed and you have no reference to them.
You should check the object is not null before trying to access it. You should add null check like if(playerBody!=null) before accessing the player’s position and if(LookPoint!=null) before trying to look at it.


Can you make the script as you say