I am working on a little game where in one Level there are multiple enemies with the Tag “Enemy” and I want the player to check if every enemy has been destroyed and if yes it should change to another Scene.
(Btw. Im a totally beginner so pls don´t judge me and I need it in like 2 weeks because its a school project(not kidding))
Thanks
Lots of ways to do it, you could have a reference to a of enemies and check if they are all null, you could have the enemy call a method OnDestroy to update the player’s info… but for your intentions, I think this is the best way. Declare a static int numberOfEnemies in your enemy class, then in Start you add number of numberOfEnemies++; in OnDestroy you add numberOfEnemies–; then you can get the number of enemies at all times from your enemy class and you have access to that variable without a reference because it’s static, static variables are shared across all instances. If you are enabling and disabling your enemies rather than initializing and destorying put the numberOfEnemies++ in OnEnable and the numberOfEnemies-- in the OnDisable. This is probably on of the simplest and low impact ways of doing it. In your player script or level manager, you use something like: if(EnemyClass.numberOfEnemies<1) LoadNextScene();
Some typing might be wrong because I dont have unity open right now. This just creates an array with every GameObject of a certain type (in this scenario if it has the tag Enemy)
GameObject[] LivingEnemies;
Update()
{
// Finds every gameobject with enemy tag and adds it to LivingEnemies Gameobject array
LivingEnemies = Find.GameObjectsOfType.CompareTag("Enemy");
if (LivingEnemies.Length == 0) // Checks if there are no LivingEnemies
{
// Changes Scene (using scene manager, forgot code for this)
}
}