check if object is destroyed ?

Hi All I have this script to spawn my object repeatedly after a delay

var Theprefab : GameObject;
var WaitTime = 1.0;

function Start () {
InvokeRepeating ("spawn", WaitTime, WaitTime);

}

function spawn () {
var Instance : GameObject = Instantiate(Theprefab, transform.position, transform.rotation);
}

It works prefectly it spawns the object and has a delay then spawns the object again but how can I check if the previous object is destroyed before spawning the new one ?

On the objects script when I click on the object it destroys itself so how would I check if the object is still in the scene before spawning the new object ?

var TheObject : GameObject;
var Theprefab : GameObject;
var WaitTime = 1.0;

function Start () {
     InvokeRepeating ("spawn", WaitTime, WaitTime);
}

function spawn () {
     if(TheObject == null) Debug.Log("Has been destroyed!");
     TheObject = Instantiate(Theprefab, transform.position, transform.rotation);
}

You should change the logic, since InvokeRepeating can’t be temporarily suspended - you can just cancel it with CancelInvoke. You could use a coroutine instead, and call Spawn only the object count is below the max (1, in this case).

About the object count: let Unity take care of this for you! The idea is simple: attach the spawning script to an empty object, which will be the parent of every spawned object; Unity always knows how much children a transform has - the childCount property is incremented and decremented automatically when a child is created or destroyed - thus you can read its childCount property to know how much objects are alive in scene. NOTE: The only restriction is that this empty object must stay static - no move, no rotation, or else all objects spawned will be moved as well!

var Theprefab : GameObject;
var WaitTime = 1.0;
var maxObjects: int = 1; // max spawned objects alive in scene

function Start () {
    while (true){ 
        // let Unity free till the end of WaitTime
        yield WaitForSeconds(WaitTime);
        // spawn objects only when there are less than maxObjects
        if (transform.childCount < maxObjects) Spawn();
    }
}

function Spawn () {
    var Instance : GameObject = Instantiate(Theprefab, transform.position, transform.rotation);
    Instance.transform.parent = transform; // make it a child of its creator
}

OhK now I have another problem :confused:

When using either one of the script there is a problem ! The first script it work fine but if I have multiple instances of the object open then the time delay doesn’t work and spawn immediatley

In the second script it does wait until the object is destroyed at all it just keeps respawning them.