Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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:

Destroy(gameObject)

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
    tempSectionControllerFlyingEnemies._numberOfFlyingEnemies.Remove(gameObject);
    //Destroy(gameObject);



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

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

 if (_groundEnemiesRemaining == 0)
        {
            MoveToNextSection();
            _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.

share|improve this question

closed as too localized by Byte56, Sean Middleditch, bummzack, Josh Petrie, Anko Apr 7 '13 at 16:33

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

1 Answer 1

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.

share|improve this answer

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