- Home /
Games with multiple levels each with an array of enemies
I'm looking for a way to design a game with multiple levels each with an array that will spawn a set amount of enemies one at a time. Once the set amount of enemies is killed by the player the level will end and they will be taken to the next level. I'm lust looking for advice on how to proceed and any code samples to look at. I would love to be able to talk back and forth with someone about the topic if they had the time on discord or something.
Answer by ZuhairGhias · Apr 28, 2018 at 06:40 AM
You could try creating a serialized class that holds information about what you want to spawn. You could then use that in the script for your level like this:
public class Script : MonoBehaviour{
public List<EnemyInfo> enemyWave;
// The rest of your functions
}
[Serializable]
public class EnemyInfo{
public GameObject enemyPrefab;
public int spawnCount;
}
This should allow you to edit your enemyWave in the editor. [Serializable] just lets you see the sub info of the class in the editor.
To progress just tag your enemy prefab with "enemy" and check if there are no enemies left in the scene using GameObject.FindGameObjectWithTag("enemy") or something similar.
Answer by fafase · Apr 28, 2018 at 08:38 AM
You need a level manager that will set the amount and type of enemies to spawn and will also know about the dead enemies.
Let's see a simplistic version of it:
public abstract class LevelManager : MonoBehaviour
{
public int requiredKills = 0;
public GameObject enemyPrefab = null;
public int spawnEnemy = 0;
protected virtual void InitLevel()
{
for(int i = 0; i < this.spawnEnemy; i++)
{
Instantiate(this.enemyPrefab);
}
Enemy.RaiseDeath+= ProcessEnemyDeath;
}
protected virtual void OnDestroy()
{
Enemy.RaiseDeath -= ProcessenemyDeath;
}
void ProcessEnemyDeath()
{
if(--this.requiredKills == 0){ EndLevel(); }
}
}
This is a base class so you would create sub classes for each level. Then drag the prefab for the enemy level so you can have different enemies as levels go up, different amount needed and so on. Since the Init method is virtual you can override it in sub classes to perform more action.
There is also an Enemy class mentioned:
public class abstract Enemy :MonoBehaviour
{
public static event Action RaiseDeath;
protected virtual void ProcessDeath() // here you handle it your way to define when the enemy is dead
{
if(RaiseDeath != null){ RaiseDeath(); }
}
}
Again, this is abstract class so you need subclass for your enemies and you can give them the amount of health or power or else to define them.
When the enemy is meant to die, the event is triggered which will notify the LevelManager that an enemy is dead.
So the level manager would be a lot like a game manager. But I'm a little confused on how the sub classes would work for each level. Would I just write a different script for every level?
Yes, the base class is abstract so you cannot use it, it has to be a base class. Each level gets its own script so you can have common behaviours in base class and specific in sub class.
Also another issue i'm having is my player and projectiles are both going through my enemy even though both have colliders.
this is another issue, either you are using Transform to move the projectile so physics is ignored or you are moving it to fast and it misses.
I was looking over your enemy class you created and i'm a little confused
There was a mistake with the event, I fixed it as it should have been static.
Your answer
![](https://koobas.hobune.stream/wayback/20220612161643im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Now how to get Started? 1 Answer
How can i make levels to a fps game 1 Answer
getting udp package info inside unity (GlovePIE) 0 Answers
How do you add levels to your game? 1 Answer
how do you put the slenderman in unity3d 0 Answers