How do I know all objects have been destroyed?


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.

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?