Detecting inactive pooled objects to end game.

Hi, I’ve been testing unity for a few days and i wanted to make a simple game to try what i’ve been learning.

The game is simple, ducks spawn randomly in the screen, if they are grounded you can teleport them whenever you click on the screen(The teleport part is on the “Player_Control” script not in this one. At first i thought of instanciating and destroying the ducks but after some tutorials I decided to try pooling.

Ducks will spawn every 5 seconds and if you manage to kill all of them before another one spawns, you win the game.

My problem is that i don’t know how to detect when all the ducks are not active so i can go to the “game over” menu. Right now I first, detect the explosion of the duck which is an instanciated prefab activated when every duck dies. Then i made corutine to count the ducks that are deactivated(This couritine is giving me an error “Cannot implicitly convert type float' to System.Collections.IEnumerator”). And finally if the Count is equal to the maximum ducks I move to the “game over” screen.

I think it’s pretty rough and it has to be a better way but i can’t find it.

Thnx for your time and help.

This is what i’ve got so far.

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Spawn_Control : MonoBehaviour {

	public int pooledAmount = 10;
	public GameObject player;
	public float timer;
	public float waitForExplosion;

	float deactivated;

	List <GameObject> ducks;

	void Start()
	{
		//Creating and filling the duck list
		ducks = new List<GameObject> ();
		for (int i=0; i < pooledAmount; i++)
		{
			GameObject obj = (GameObject) Instantiate(player);
			obj.SetActive(false);
			ducks.Add(obj);
			Debug.Log ("We have: "+ ducks.Count + " ducks");
		}
		//Spawning ducks
		InvokeRepeating ("Spawn", 1, timer);
	}

	void LateUpdate()
	{
		//Checking for explosions.
		GameObject explosion= GameObject.Find("Explosion");
			if (explosion)
			StartCoroutine (GameOver(waitForExplosion));
	}

	void Spawn ()
	{
		//Simple random instanciation of ducks
		for (int i=0; i < ducks.Count; i++) 
		{
			if (!ducks*.activeInHierarchy)*
  •  	{*
    
  •  		Debug.Log("Duck "+i+" out!");*
    
  •  		Vector3 screenPosition = Camera.main.ScreenToWorldPoint (new Vector3 (Random.Range (0, Screen.width), Random.Range(0,Screen.height), Camera.main.farClipPlane / 2));*
    

_ ducks .transform.position = screenPosition;_
_ ducks*.SetActive(true);
break;
}
}
}*_

* //Check if all ducks are deactivated.*
* //Wait so the animation is complete*
* //To the “game over” screen.*
* IEnumerator GameOver(float waitTime)*
* { StartCoroutine (Count(deactivated));*
* if (deactivated == 11)*
* {*
* yield return new WaitForSeconds (waitForExplosion);*
* Application.LoadLevel (1);*
* }*

* }*

* //Checking hoy many ducks are not active*
* IEnumerator Count (float deactivated)*
* {*
* for (int i=0; i < ducks.Count; i++)*
* {*

_ if (!ducks .activeInHierarchy)
* {
deactivated–;
}
else*

* {
deactivated++;
}
return deactivated;
}
}
}*_

Some tweaks to your GameOver method and everything below that. Generally its better to generate a bool for your check, rather then comparing ints. This way if the number of ducks changes the method will still work. Let me know if you need assistance figuring out how this works.

IEnumerator GameOver(float waitTime) 
{
    while (!CheckForGameOver) yield return null;
    yield return new WaitForSeconds (waitTime);
    Application.LoadLevel (1);
}

public bool CheckForGameOver () {
    foreach (GameObject duck in ducks){
        if (duck.activeSelf) return false;
    }
    return true;
}