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.