Reputation: 79
I just don't know how to write the if part. The rest is just instantiate position and quaternion stuff.... Probably.
I want to know if the GameObject rocketspawnblue and GameObject rocketspawnred are gone, so I can spawn the next pair until all the pairs in the loop are spawned.
This is my spawning coroutine I put in Start()
. however that's probably won't work because it needs to be check every frame until i take all my rockets....
for (int i = 1; i < 6; i++)
{
yield return new WaitForSeconds(1);
GameObject rocketspawnblue = Instantiate(
rocketblue,
new Vector3(Random.Range(-15, 15), (Random.Range(-15, 15))),
Quaternion.identity);
SpriteRenderer rocketscolor1 = rocketspawnblue.GetComponent<SpriteRenderer>();
//rocketscolor.color = colors[Random.Range(0,colors.Length)];
rocketscolor1.color = Color.blue;
GameObject rocketspawnred = Instantiate(
rocketred,
new Vector3(Random.Range(-15, 15), (Random.Range(-15, 15))),
Quaternion.identity);
SpriteRenderer rocketscolor2 = rocketspawnred.GetComponent<SpriteRenderer>();
//rocketscolor.color = colors[Random.Range(0, colors.Length)];
rocketscolor2.color = Color.red;
}
Upvotes: 2
Views: 252
Reputation: 20259
Use a List
as a field to track the rockets:
private List<GameObject> rockets;
At the beginning of Start()
, instantiate the list:
rockets = new List<GameObject>();
In your coroutine, clear the list with Clear
then use Add
to add rockets to your list. After you're done adding, use WaitWhile
to loop until there are no rockets left. We'll write a method called AnyRocketsLeft
that returns true if there are any that are not yet destroyed.
After the WaitWhile
, use WaitForSeconds
to wait for a second to give time between batches.
for (int i = 1; i < 6; i++)
{
rockets.Clear();
GameObject rocketspawnblue = Instantiate(
rocketblue,
new Vector3(Random.Range(-15, 15), (Random.Range(-15, 15))),
Quaternion.identity);
SpriteRenderer rocketscolor1 = rocketspawnblue.GetComponent<SpriteRenderer>();
//rocketscolor.color = colors[Random.Range(0,colors.Length)];
rocketscolor1.color = Color.blue;
rockets.Add(rocketspawnblue);
GameObject rocketspawnred = Instantiate(
rocketred,
new Vector3(Random.Range(-15, 15), (Random.Range(-15, 15))),
Quaternion.identity);
SpriteRenderer rocketscolor2 = rocketspawnred.GetComponent<SpriteRenderer>();
//rocketscolor.color = colors[Random.Range(0, colors.Length)];
rocketscolor2.color = Color.red;
rockets.Add(rocketspawnred);
yield return new WaitWhile(() => AnyRocketsLeft());
yield return new WaitForSeconds(1);
}
// ran out of rockets
// set up the "next level" prompt, etc. here
To check if any rockets are not yet destroyed, we can check their activeInHierarchy
property. Just loop through the list of rockets and return true if you find any, and false otherwise.
private bool AnyRocketsLeft() {
foreach (int i = 0 ; i < rockets.count ; i++) {
if (rockets[i].activeInHierarchy) return true;
}
return false;
}
If you're using Linq, you can use List.Any():
private bool AnyRocketsLeft() {
return rockets.Any(o => o.activeInHierarchy);
}
Upvotes: 2