- Home /
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 ?
Answer by aldonaletto · Oct 29, 2011 at 01:01 PM
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 }
Thank you so much I would have never thought of using childcount m
Now the script works great but when I spawn the object for the first time it spawns immediatley how can I make it have the same delay as when it respawns?
Oh, I forgot to change the order of the lines: place yield first and the if after it. I modified the answer to fix this.
@Donovan_Rucastle, if this answer solved your problem, please click the accept button (the check mark button below the voting thumbs) - this will help others to find the solution for similar problems.
Answer by col000r · Oct 29, 2011 at 12:34 PM
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);
}
Answer by Donovan_Rucastle · Oct 30, 2011 at 04:02 PM
OhK now I have another problem :/
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.
You see what you did just there? Ins$$anonymous$$d of closing off this question and opening a new one, you posted a comment as an answer and didn't mark either of the existing answers as accepted.
Don't do that.
@syclamoth is right: posting comments/replies as answers mess things up.
Anyway, I tested my script and it works fine - a new object is spawned only after the other has been destroyed.