0
votes

Rundown Ok guys and gals need a little help with this. Basically I'm instantiating a healthPack from a prefab in an open position. After a certain time elapses I am trying to destroy the HealthPack if it isn't picked up, set a bool healthPackExist back to false, and instantiate another healthPack at a free position.

Problem: When I've attempted to access the gameObject being instantiated I end up either destroying the whole Parent Hierarchy or just wiping the script out.

Solutions I've tried destroying the root object, searching for the name of the created object, adding a tag to the object (Health Pack) and searching for it always getting errors.

Code is below:

public GameObject healthPackPrefab;
public GameObject health;
private float healthTimer;
private bool healthExist;

// Use this for initialization
void Start () 
{
    //set healthExist to false to indicate no health packs exist on game start
    healthExist = false;
}

// Update is called once per frame
void Update () 
{
    //first check to see if a health pack exist, if not call method to spawn a health pack
    //otherwise check to see if one exist and if it does is it's timer created with it
    //has been passed by Time.time (returns game clock time), if yes destroy the created
    //health pack.
    if (healthExist == false) 
    {
        spawnUntilFull ();
    } 
    else if (healthExist == true && Time.time > healthTimer)
    {
        //Error occuring here when trying to destroy
        //Destroy(transform.root.gameObject)    //destroys script, object scripts on, and prefab
        Destroy(this.gameObject);   //does same as Destroy(transform.root.gameObject
        healthExist = false;
    }
}



Transform NextFreePosition()
{
    //free space
    foreach (Transform childPositionGameObject in transform) 
    {
        //if no health packs located return location of child object to spawn new health pack
        if (childPositionGameObject.childCount == 0) 
        {
            return childPositionGameObject;
        }
    }

    return null;
}

void spawnUntilFull()
{
    //returns next free position in space
    Transform freePosition = NextFreePosition ();

    //if free slot is available
    if (freePosition && healthExist == false) 
    {
        //instantiates health object and places it in scene at coordinates received from
        //freePosition
        health = Instantiate (healthPackPrefab, freePosition.position, Quaternion.identity) as GameObject;

        //spawns enemy onto a position under the Parent (Enemy Formation)
        health.transform.parent = freePosition;

        //set bool to true to stop spawning
        healthExist = true;

        //seat HealthTimer to 5 seconds after creation for use in Update method
        healthTimer = Time.time + 5.0f;
    }
}
2
are you only trying to create one object? your code 'open position' and 'spawnUntilFull' makes it sound like you want to spawn more than one, but your code appears to only allow one to be created because you change healthExist to true.ethan codes
I'm leaving it open for now to create them randomly based off time and enemies destroyed. That's why when it's either picked up by player (different script) its destroyed and changed to healthExist = false, and then again in the above script when the timer runs out it is supposed to be destroyed to allow for another creation later.Phillipv20

2 Answers

2
votes

What you are effectively doing when you call Destroy() is destroying the script. To achieve what you want (destroying the health pack) you simply have to call Destroy on it instead:

Destroy(health);

On a side note, to avoid messy code using Time.time, Destroy() has an overload that uses two parameters:

Destroy(GameObject object, float delay);

This should help simplify your code and make it more readable.

0
votes

You could add a separate script on the prefab with your custom DestroyObject method in it.

Start a timer on that script as soon as the object is created.

Now if it is not collected within a certain time, you could easily destroy the object.