- Home /
How do I know all objects have been destroyed?
Hello
I have three targets in a scene made of cubes, called target1, target2, and target3. They are destroyed on collision with a RigidBody.
How do I know when these three objects are gone so I can call my 'Game Over' state?. They aren't prefabs or dragged onto anything in the inspector right now, and their only component is the code to destroy themselves on collision.
Thanks in advance.
UPDATE
Thanks to Screenhog's answer below, I was able to stop my counter counting up when all the targets are destroyed. Here's the final code for reference.
private var timeElapsed:float = 0f;
var targets:Target[];
function Update(){
if (targets.length > 0){
timeElapsed += Time.deltaTime;
}
else {
print("Clock has stopped");
}
targets = FindObjectsOfType(Target) as Target[];
print("Target length is " +targets.length);
}
This trick of using a unique component to identify a group of objects and then using FindObjectsOfType is another really nice piece of the Unity puzzle slotting into place.
Thanks for the help.
Answer by Screenhog · Sep 12, 2012 at 07:38 PM
I would find a way, through scripting, to populate them in an array (giving them all a particular script component and using FindObjectsOfType is a good way). When one is destroyed, remove it from the array, and then do a test to see if any objects are still in the array. If the length of the array is 0, then it's Game Over.
That's the overview... is that enough to help, or do you need more detail?
Thanks Screenhog. Thats a really good challenge to get my head around (bit of a n00b). And the tip of using a component to round up a bunch of similar objects is a really handy insight. I'll give it a go now and let you know how it goes.
As you can see above, I got it working. Thanks again. It's always better to be pointed in the right direction and then find the answer, rather than to be spoon-fed. I feel like I really learned something.
I wasn't sure what the forum protocol was for including the correct code, whether it should be a new answer or an update to the original question, so I took a punt and edited the OP.... didn't want to s$$anonymous$$l you answer glory :)
Be careful FindObjectsOfType is a very slow procedure and you call it every frame. You should populate the array when you create a target ins$$anonymous$$d of serching them after.
I know what you mean, it is kind of hacky. Ideally, I would delete the target from the array upon destruction, then just check for length on Update(). Unfortunately, I haven't got my head around how to access the timer script (where the array is declared) from the target so it pops itself out of the array bfore destruction.
In fact, getting a script to talk to a script on another object is a cause of about 75% of my confusion with Unity. I can't wait for that piece to pop into place. Thanks for the comment though, I will get better :)
Your answer
Follow this Question
Related Questions
How to make gameobjects fall down faster with each update ? 1 Answer
Recalculating collisions after Physics2D.IgnoreLayerCollision()? 1 Answer
Addforce to ball in camera position 0 Answers
AddForce doesn't work when used with velocity 3 Answers
How can I rotate around with a Rigidbody while respecting physics? 0 Answers