need help with an error code and how to fix it

Hi, im getting this error code.
MissingReferenceException: The object of type ‘GameObject’ 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. And this error code is occuring in this code. PLS help ):

using UnityEngine;

public class Gun : MonoBehaviour {

public float damage = 10f;
public float range = 100f;

public Camera fpsCam;
public ParticleSystem muzzleFlash;
public GameObject impactEffect;

// Update is called once per frame
void Update () {

if (Input.GetButtonDown(“Fire1”))
{
Shoot();
}

}

void Shoot ()
{
muzzleFlash.Play();
RaycastHit hit;
if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range))
{
Debug.Log(hit.transform.name);

Target target = hit.transform.GetComponent();
if (target != null)
{
target.TakeDamage(damage);
}

Instantiate(impactEffect, hit.point, Quaternion.LookRotation(hit.normal));
}
}
}

First, use CODE tags when posting code.

How do you know the error is occurring within this code? Did the error include a line number? That’s as important as the error text, since it tells you exactly where in this code the error occurred. Always include the line and character number from the error if it’s provided.

Usually, as the error message indicates, this error occurs because you’re trying to access a game object after it has already been destroyed. I can’t tell which object that might be. Are you calling Destroy() on any objects in another script? Maybe fpsCam is destroyed, or muzzleFlash is destroyed? I don’t think raycast will hit destroyed objects, so I’d assume the issue is with fpsCam, but I can’t tell.

using UnityEngine;

public class Target : MonoBehaviour
{
    public float health = 50f;

    public void TakeDamage(float amount)
    {
        health -= amount;
        if (health <= 0f)
        {
            Die();
        }

        void Die()
        {
            Destroy(gameObject);
        }
    }
}

do u think its in this script destroying the object?

I don’t know. Again, did the error message show the line numbers? If you click on the message in the console, it usually shows a full stack trace with line numbers. That would help inform which object is destroyed. I don’t even know, yet, that it’s coming from the script you posted above, since I haven’t seen the stack trace.

You’ve got some code here that’s correctly checking for null before accessing the object. For example, you do that with “target != null”. Generally, you probably just need another test like that, to make sure the object isn’t null. The way things work in Unity is that if you destroy an object (let’s call it “myObject”), and then try to access any field/property on the object, it will give you the error you’re seeing. But you can also simply test for “if (myObject != null)” to make sure it’s neither null nor destroyed. Unity overloads == and != for game objects to not only test if they’re a null reference, but to also test to make sure the object isn’t currently destroyed.

So, it would be helpful to know exactly which line is erroring, at which point you have the choice: Either just perform a null test for that object, or track down what’s destroying the object and prevent it from being destroyed if you don’t think it should have been destroyed yet.

Hi, thank you for the help but i have sorted it out it works, thank you