How to check if a GameObject has been destroyed?

public GameObject shot;
public Transform shotSpawn;
public float fireRate;
public float delay;
public GameObject enemyShipMedium;

void Fire()

{
    if (GameObject.enemyShipMedium == null)
    {
        return;
    }
    else
    {
   Instantiate(shot, shotSpawn.position, shotSpawn.rotation);

}
void Start()

{

    if (enemyShipMedium == null)
    {
        return;
    }
    else
    {
              InvokeRepeating("Fire", delay, fireRate);

}

With this code I am trying to get an enemy ship shooting at the player omitting all irrelevant code, the problem is that I get error "the GameObject is destroyed, but you are still trying to access it, even though I have tried to check if the GameObject is null and return (leave the function for that particular enemy) if the enemy is destroyed. My attempts to debug this problem seems to indicate that the GameObject is null in the first frame just before the enemy enters the screen (in another script ommitted here) and because the GameObject is null the game acts irregular preventing the enemy GameObject from firing. Could someone please tell me how I check if a particular Gameobject one or more total is null and prevent the rest of the code executing for that particular enemy GameObject possibly for a total of a large amount of enemies preventing destroyed enemies from reaching the shooting code. Thanks.

Don’t reference the gameobject directly. Add a bool for alive or dead and change that instead of checkign the gameobject. Create a variable alive of the type bool and set it to true in your script (This script should eb attached to a gameobject other than the ship which gets destroyed). At the point where you destroy the ship (probably in OnCollisonEnter or OnTriggerEnter), before destroying the ship, reference the script and change the value of alive to false. And in the Fire function check whether alive is true or false instead of checking the gameobject.

I got it, I got it, I found a really good solution and I will answer my own question to help others. The only change I made was:

void Fire()

{

if(!enemyShipMedium.activeSelf)

{

Instantiate(shot, shotSpawn.position, shotSpawn.rotation);

}

else

{

return;

}

}

And now my game works perfectly.