"the object of type transform has been destroyed but you are still trying to access it" problem,

The problem is line 8(null reference)

private void Start()
{
    timeBtwShots = startTimeBtwShots;
}

private void Update()
{
    if (target.position != null)
    {
        Vector3 difference = target.position - transform.position;
        float rotZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
        transform.rotation = Quaternion.Euler(0f, 0f, rotZ + offset);
    }

    if (timeBtwShots <= 0)
    {
        Instantiate(projectile, shotpoint.position, transform.rotation);
        timeBtwShots = startTimeBtwShots;
    }else
    {
        timeBtwShots -= Time.deltaTime;
    }
}

}

,

Hi caprariualex,

The error you are seeing means the object you’re trying to access has been destroyed somewhere else in your code.

If the error is from line 8 then the only object there is the target. I assume this is a target transform.

It’s likely that somewhere else in your code you’ve called Destroy() with the gameobject that the target transform belongs to.

Try adding a null check on line 8 for the target. Something like:

if (target!=null)

The code you currently have is checking the struct’s position for null and will never return false as a transform’s position is a struct, which is a value type, and can never be null.

I hope this helps.