Change level once killed enemy
Hi guys I want to make it so the scene can change to another scene once i killed the last enemy. Here's my enemy spawner script
public GameObject enemy;
public Transform[] spawnPoints;
public Vector3 spawnValues;
public int howoften = 10;
public int howmany = 3;
public float etimer;
void Update()
{
etimer = etimer - Time.deltaTime;
if (etimer < 0)
{
etimer = howoften;
int i = howmany;
while (i > 0)
{
i--;
int spawnPointIndex = Random.Range(0, spawnPoints.Length);
Instantiate(enemy, spawnPoints[spawnPointIndex].position, spawnPoints[spawnPointIndex].rotation);
}
}
}
Answer by Koyemsi · Apr 04, 2018 at 09:54 AM
Maybe like this :
while (i > -1)
{
i--;
if (i > 0) {
int spawnPointIndex = Random.Range (0, spawnPoints.Length);
Instantiate (enemy, spawnPoints [spawnPointIndex].position, spawnPoints [spawnPointIndex].rotation);
} else {
SceneManager.LoadScene ("myScene");
}
}
it dose change scenes but it changes as soon as the game starts
Answer by Gordrik · Apr 04, 2018 at 09:50 AM
private int sceneID;
private GameObject[] enemyCount;
void Start()
{
enemyCount = GameObject.FindGameObjectsWithTag("Enemy");
sceneID = SceneManager.GetActiveScene().buildIndex;
}
private void Update()
{
if (enemyCount.Lenght <= 0)
{
LoadOnLastKilled(SceneName);
}
}
public void LoadOnLastKilled(string name)
{
{
SceneManager.LoadScene(name);
}
}
or to automatically load next level use :
public void LoadNextScene()
{
SceneManager.LoadScene(sceneID+1);
}
To really make it work, you need to reduce enemyCount in Update()
method every time the enemy object is destroyed, if you have any troubles with implementation, let me know.
i get an error on LoadOnLast$$anonymous$$illed when i put it on update
if (enemyCount.Length <= 0) { LoadOnLast$$anonymous$$illed(); }
Assets/Scripts/Level/Spawner.cs(47,13): error CS1501: No overload for method LoadOnLast$$anonymous$$illed' takes
0' arguments
Answer by tormentoarmagedoom · Apr 04, 2018 at 09:53 AM
Goodday.
First, use
etimer -= Time.deltaTime
Its the same of what you use.
Second, i recommend you to create a new tag called enemy, and assignt this tag to all enemies via inspector. Then just need to.
GameObject[] enemylist = GameObject.FindObjectswithtag("enemy) if (enemylist.lenght == 0) { sceneManager.LoadScene("scenename")}
Look that the command is gameobjecS with "s". Also care with my typing, im writing from mobile so some comands have CAPS leters.
Bye!
Accept the answer if helped!
Your answer
Follow this Question
Related Questions
NullReferenceExeption 0 Answers
Enemy Spawn waves, not stop keep looping 0 Answers
wave spawner difficulty increase 1 Answer
make enemy cars spawn faster over time! 2 Answers
Linking projectiles to enemy 0 Answers