- Home /
SpaceShooter Win the game!
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?
first of all to find where your problem is try putting a a print statement in the WinGame function to see if the code is even going there.
public void WinGame()
{
print("if i see this in the console i know that this function is even running!!!");
GameObject[] enemies = GameObject.FindGameObjectsWithTag("Enemy");
for (int i = 0; i < enemies.Length; i++)
{
Destroy(enemies[i]);
}
win = true;
winText.text = "You Win!!!!!";
gameOver = true;
}
Answer by PolyvivalStudios · Jul 28, 2018 at 03:20 PM
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); }
Answer by MT369MT · Jul 28, 2018 at 03:48 PM
Hi you could use:
if (score >= 100)
{
GameObject[] Enemies = GameObject.FindGameObjectsWithTag("Enemy");
foreach(GameObject Enemy in Enemies)
{
destroy(Enemy);
}
}
Answer by tatamark84 · Jul 30, 2018 at 05:16 PM
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;
}
}
You should probably try to disable your wave spawner or just disable this script on the "WinGame" function. Example:
First, add a reference to this script:
public GameController GC;
Second, Disable GC;
public void WinGame() { GameObject[] Enemies = GameObject.FindGameObjectsWithTag("Enemy"); foreach(GameObject Enemy in Enemies) { Destroy(Enemy); } win = true; winText.text = "You Win!!!!!"; gameOver = true; GC.enabled = false; }
Your answer
![](https://koobas.hobune.stream/wayback/20220612171704im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
End game after 120 seconds HELP 1 Answer
gameover script 2 Answers
When my car crashes into the guards, the game is supposed to be over! 0 Answers