Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by mattkell462 · Apr 28, 2018 at 05:48 AM · gamelevelsarray of gameobjectsstarting

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image mattkell462 · Apr 28, 2018 at 03:34 PM 0
Share

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?

avatar image fafase mattkell462 · Apr 28, 2018 at 09:12 PM 0
Share

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.

avatar image mattkell462 · Apr 28, 2018 at 03:44 PM 0
Share

Also another issue i'm having is my player and projectiles are both going through my enemy even though both have colliders.

avatar image fafase mattkell462 · Apr 28, 2018 at 09:12 PM 0
Share

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.

avatar image mattkell462 · Apr 28, 2018 at 08:32 PM 0
Share

I was looking over your enemy class you created and i'm a little confused

avatar image fafase mattkell462 · Apr 28, 2018 at 09:13 PM 0
Share

There was a mistake with the event, I fixed it as it should have been static.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

114 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges