Too subjective and argumentative
,How do I code a script that player need to destroy all of enemies in all waves to move to the next scene.
Hi I'm facing the problem of making a script that the player need to kill all of the enemies in in every waves before the player could automatic move to the next scene. Since the code that I have I could only kill one of the enemy and it move to the next scene instead of need to kill all of enemies. So how do I create the connection between the SpawnManager so that the player can kill all of the enemy in the waves and then progress to the next level instead on kill one and progress.
GameController
[SerializeField] Button newGameButton = default;
[SerializeField] Button quitGameButton = default;
[SerializeField] Canvas theMainMenu = default;
public static GameController gameController;
public int playerHP;
public int playerScore;
private void Awake()
{
if (gameController == null)
{
DontDestroyOnLoad(gameObject);
gameController = this;
}
else if (gameController != this)
{
Destroy(gameController);
}
newGameButton.onClick.AddListener(StartNewGame);
quitGameButton.onClick.AddListener(QuitGame);
}
private void StartNewGame()
{
theMainMenu.gameObject.SetActive(false);
SceneManager.LoadScene(1);
}
private void QuitGame()
{
Application.Quit();
if (UNITY_EDITOR)
UnityEditor.EditorApplication.isPlaying = false;
return;
endif
}
public void RestartLevel()
{
playerHP = 100;
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
public void LoadLevel()
{
int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
if (currentSceneIndex == 3)
currentSceneIndex = 0;
else
currentSceneIndex++;
SceneManager.LoadScene(currentSceneIndex);
}
}
SpawnManager
[SerializeField] int numberOfEnemies;
private GameObject ourPlayer;
private PlayerHud ourHudScript;
public GameObject ourEnemy;
private int score = 0; //beginning score
public int waveCount = 1; // beginning wave
List<GameObject> enemies; // list to hold enemies
public int deadEnemy = 0; // list to hold dead enemies
[SerializeField] List<GameObject> enemyStart = default;
void Start()
{
//Get (or find) a reference to the player so we can tell the HUD how many enemies are needed to progress the wave
ourPlayer = GameObject.FindGameObjectWithTag("Player");
ourHudScript = ourPlayer.GetComponentInChildren<PlayerHud>();
enemies = new List<GameObject>(); //create list to hold enemies and instantiate them into the scene upon start
spawnEnemies(numberOfEnemies); //spawn enemies based on the number of enemies
ourHudScript.ShowScore(score, numberOfEnemies); //present score to hud on start
ourHudScript.ShowWave(waveCount); //present waveCount to HUD
}
public void RemoveEnemy(GameObject deadEnemy) //remove enemies if destroyed and increment the kill counter
{
score++; //increment score
Destroy(deadEnemy); //destroy enemy
enemies.Remove(deadEnemy); //bring out your dead
//this.deadEnemy++; //specify the dead to remove
if (score == numberOfEnemies)
waveCount++;
ourHudScript.ShowScore(score, numberOfEnemies); //show score on screen
ManageWaves(); //manage the waves
}
private void ManageWaves() //wave manager
{
if (deadEnemy == 3) //if the dead enemies = 3
{
waveCount++; //increase the wave count by 1
numberOfEnemies = (4); //set the text for number of enemies to kill to 4
spawnEnemies(4); //increase the starting enemy number (3) by 1.. to (4)
score = 0; //reset current score to 0
ourHudScript.ShowWave(waveCount); //display the wave count
}
else if (deadEnemy == 7) //if the dead enemies = 7
{
waveCount++; //increase the wave count by 1
numberOfEnemies = (5); //set the text for number of enemies to kill to 5
spawnEnemies(5); //increase the starting enemy number of the previous itteration (4) by 1.. to (5)
score = 0; //reset current score to 0
ourHudScript.ShowWave(waveCount); //display the wave count
}
else if (deadEnemy == 12)
{
//display you win text winText.text
}
GameController.gameController.LoadLevel();
}
private void spawnEnemies(int numEnemy) //spawn enemy army
{
for (int i = 0; i < numEnemy; i++) //iterate enemy count by 1 each time
{
enemies.Add(Instantiate(ourEnemy, enemyStart[i].transform.position, enemyStart[i].transform.rotation)); //instantiate enemies and set their direction
}
}
}
PlayerHud
public Canvas ourCanvas;
public Text ammoText;
public Text useText;
public Text lootText;
public Text healthText;
public Image screenOverlay;
public string displayText = "";
public Text waveText;
public Text scoreText;
public SpawnManager ourSM;
private PlayerController ourPC;
//set items that should function on start
void Start()
{
ourPC = GetComponentInParent<PlayerController>();
//ourSM = GetComponentInParent<SpawnManager>();
useText.gameObject.SetActive(false);
lootText.gameObject.SetActive(false);
screenOverlay.gameObject.SetActive(false);
}
void Update()
{
ammoText.text = "Ammo : " + ourPC.ammoCount + " / 100"; //update ammo amount for player
healthText.text = "Health : " + GameController.gameController.playerHP; //update health for player
waveText.text = "Wave : " + ourSM;
if (ourPC.showUse) //display usable items
ShowTheUseTip(true);
else
ShowTheUseTip(false);
if (ourPC.isDead) //display red screen of death if player is dead
{
screenOverlay.color = new Vector4(1f, 0f, 0f, 0.5f);
screenOverlay.gameObject.SetActive(true);
}
else //otherwise set the activity of the screen overlay to !active
{
screenOverlay.gameObject.SetActive(false);
}
}
private void ShowTheUseTip(bool value) //is the object active yes/no
{
useText.text = displayText; //display text
useText.gameObject.SetActive(value); //set text as active
}
public void DisplayTheLoot(string theText) //display the loot text
{
StartCoroutine(DisplayLoot(theText));
}
IEnumerator DisplayLoot(string theText) //loot the loot
{
lootText.text = theText; //link to loot text
lootText.gameObject.SetActive(true); //set text as active
yield return new WaitForSeconds(5f); //
lootText.gameObject.SetActive(false); //make text disappear after xx sec
}
public void ShowScore(int score, int outOf) //update the scoreText when an enemy is removed
{
scoreText.text = "Score : " + score.ToString() + "/" + outOf.ToString(); //set the score information from the spawn manager
}
public void ShowWave(int wave) //update the scoreText when an enemy is removed
{
waveText.text = "Wave : " + wave.ToString() + "/" + "3"; //set the score information from the wave manager inside the wave manager
}
}
Answer by tormentoarmagedoom · Mar 13, 2020 at 10:01 PM
FAQ :
What are the guidelines for writing good questions?
We want Unity Answers to become a comprehensive database of questions and answers related to Unity. When you ask your questions, it can be helpful to keep a few things in mind:
Don't write things that will be irrelevant in a few days. Writing that "This is urgent!" will probably not get you faster answers, but it may make people frown at your question.
Some reasons for getting a post rejected:
There exists duplicate questions with answers if you were to do a search, either on Answers or on the Forum or on Unity's tutorials
Your question isn't specific enough: asking for a script, asking multiple questions in one post or asking a question that could be either subjective or require extensive discussion
Its like you downloaded a full game, came here to UA and ask for people to modify it to get what you want.
Bye Bye! post closed.
Follow this Question
Related Questions
Use a string to call a class 2 Answers
How to, with a button, switch to another level when all items are collected? 1 Answer
how to make level go to next 1 Answer
How to have a Next Level and Game Over pop up in scene please help! 1 Answer
Hi! I need a JavaScript that allows me to go to the next level after I have reached a condition. 0 Answers