// Use this for initialization void Start() { //First we get all the asteroids in children, then we use static class AsteroidCounter to hold the number of asteroids asteroids = GetComponentsInChildren <AsteroidScript> (); AsteroidCounter.setCounter(asteroids.Length); StartCoroutine("AttackPlayer"); }
//after a specified delay, choose a random asteroid and send it towards the player IEnumerator AttackPlayer() { //makes sure that all the asteroids haven't been destoryed if (AsteroidCounter.counter == 0) { yield break; } yield return(new WaitForSeconds(time)); //Since the player can destroy asteroids at any time, //we get the asteroids in children again to prevent using an asteroid that's been destroyed asteroidsArray = GetComponentsInChildren <AsteroidScript> (); AsteroidCounter.setCounter(asteroidsArray.Length); int num = Random.Range(0, asteroidsArray.Length); if (asteroidsArray.Length > 0 && asteroidsArray [num] != null) { asteroidsArray [num].Attack(); } //Debug.Log ("Asteroid number " + num + " is attacking!!"); StartCoroutine("AttackPlayer"); }