I'm trying to have my game spawn enemies when ever the player reaches a way point.

Right now, I have this functionality working. When my player gets to the first way, the enemies spawn. He only moves on once he has killed all of the enemies on the screen are dead. However, when he gets to the second way point, no enemies spawn.

Right now, in my collision class I call the following line of code:


Whilst this work for the first way point, the second one wont spawn anything as the game object my collision class has been attached to has been destroyed. However, all of my enemies are prefabs and I thought the destroy function would only destroy that instance of the prefab. No matter when you called the instantiate method, it would only destroy that instance.

I'm spawning my enemies with the following method:

  public GameObject SpawnEnemies()
    Vector3 _position = new Vector3(transform.position.x, transform.position.y, transform.position.z);

    // instantiate particel system
    Instantiate(_particle, _position, Quaternion.identity);

    _clone = (GameObject)Instantiate(_enemy, _position, transform.rotation);
    _ai = _clone.GetComponent<HV_BaseAI>();
    _ai._waypoints = _wayPoints;

    return _clone;

Then I'm finding out how many of the enemies are still alive with the following code in my collision method:

  GameObject g, f; // enemies I want to spawn
    g = GameObject.FindGameObjectWithTag("SectionController");
    f = GameObject.FindGameObjectWithTag("SectionController");

    HV_SectionController tempSectionControllerGroundEnemies, tempSectionControllerFlyingEnemies;
    tempSectionControllerGroundEnemies = g.GetComponent<HV_SectionController>();
    tempSectionControllerFlyingEnemies = f.GetComponent<HV_SectionController>();

    tempSectionControllerGroundEnemies._numberOfGroundEnemies.Remove(gameObject); // remove enemies from list

    _numberOfGroundEnemies = tempSectionControllerGroundEnemies._numberOfGroundEnemies.Count;
    _numberOfFlyingEnemies = tempSectionControllerFlyingEnemies._numberOfFlyingEnemies.Count;

Then when I want to move on I do the following check:

 if (_groundEnemiesRemaining == 0)
            _sectionStartTime = Time.time;

i know the above line is checking only one type of enemy at the moment, but its the ground enemies I'm having issues with at the moment.

Does anyone know how I can delete the enemy prefab I'm spawning from my first section, once they've been hit, then have it respawn at the next section without the error:

The object of type 'GameObject' has been destroyed but you are still trying to access it.


If you destroy your gameObject you need to re-instantiate it at some point. If you access something that's been destroyed, you'll get the access error you're seeing.

You don't show how or when you're first instantiating gameObject. I see you're using static references to GameObject, and setting g and f to instances of a GameObject, but never gameObject.

You can either just destroy the reference, or you can implement a disable functionality that just puts the instance to sleep (removing it from the world), where it can be reused in a future spawner.


Not the answer you're looking for? Browse other questions tagged or ask your own question.