I expand a little Space Shooter. When the player arrives the score “100”, wins the game. when winning the game, I want to Destroy all “Enemy” tag immediately, but the game continues until all hazards come down. and maybe hit the player. Can you help me?
[121672-gamecontroller-win.txt|121672]
I’m not an expert, but you could try to replace the for loop with a foreach loop. @tatamark84
Also, Please Post The Full Code 
Example:
foreach(GameObject enemy in enemies)
{
destroy(enemy);
}
Hi you could use:
if (score >= 100)
{
GameObject[] Enemies = GameObject.FindGameObjectsWithTag("Enemy");
foreach(GameObject Enemy in Enemies)
{
destroy(Enemy);
}
}
Actually, when winning the game, some enemies continue coming. I click on enemies, then all of them destroyed. But new enemies come again until they finished. I don’t want this happened. I want to destroy all of the enemies forever.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class GameController : MonoBehaviour
{
public GameObject[] hazards;
public Vector3 spawnValues;
public int hazardCount;
public float spawnWait;
public float startWait;
public float waveWait;
public Text scoreText;
public Text restartText;
public Text gameOverText;
public Text levelText;
public Text winText;
public ParticleSystem ps;
private int score;
private bool gameOver;
private bool restart;
private bool win;
private int level = 1;
void Start()
{
gameOver = false;
restart = false;
win = false;
restartText.text = "";
gameOverText.text = "";
levelText.text = "Level " + level;
winText.text = "";
score = 0;
UpdateScore();
StartCoroutine(SpawnWaves());
Screen.SetResolution(600, 900, true);
}
void Update()
{
if (restart)
{
if (Input.GetKeyDown(KeyCode.R))
{
SceneManager.LoadScene("Main");
}
}
if (Input.GetKeyDown(KeyCode.Escape))
{ Application.Quit(); }
}
IEnumerator SpawnWaves()
{
yield return new WaitForSeconds(startWait);
while (true)
{
for (int i = 0; i < hazardCount; i++)
{
GameObject hazard = hazards[Random.Range(0, hazards.Length)];
Vector3 spawnPosition = new Vector3(Random.Range(-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z);
Quaternion spawnRotation = Quaternion.identity;
Instantiate(hazard, spawnPosition, spawnRotation);
yield return new WaitForSeconds(spawnWait);
}
yield return new WaitForSeconds(waveWait);
if (gameOver)
{
restartText.text = "Press 'R' for Restart or Esc to Exit!";
restart = true;
break;
}
}
}
public void AddScore(int newScoreValue)
{
score += newScoreValue;
UpdateScore();
}
if (score >= 100)
{
WinGame();
}
}
void UpdateScore()
{
scoreText.text = "Score: " + score;
}
public void GameOver()
{
gameOverText.text = "Game Over!";
gameOver = true;
}
public void WinGame()
{
GameObject[] Enemies = GameObject.FindGameObjectsWithTag("Enemy");
foreach(GameObject Enemy in Enemies)
{
Destroy(Enemy);
}
win = true;
winText.text = "You Win!!!!!";
gameOver = true;
}
}