Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Hedonsoft · May 08, 2012 at 07:58 PM · enemyspawn

Spawning different enemies for different levels

Hello everyone. I am making a 2D shooter and I want to introduce new enemies after certain levels are reached. I created a GameObject[] called enemies. I added 2 enemy prefabs to the Array. It's working except that when level 6 is reached, I want to ONLY start spawning enemy[1] not enemy[0] but it keeps spawning both. Can anyone see what I'm doing wrong?

 function LevelEnd(){
 
     EnemySpeed += -.1;
     
     if(level <=5){
         while(totalEnemies < level){
         posX = Random.Range(-4.213592,4.336067);
     
         yield WaitForSeconds(3);
         Instantiate(enemies[0], Vector3(posX, 0.1, 3.545458),transform.rotation);
         }
     }
     if(level >= 6 && level < 10){
         while(totalEnemies < level){
         posX = Random.Range(-4.213592,4.336067);
     
         yield WaitForSeconds(3);
         Instantiate(enemies[1], Vector3(posX, 0.1, 3.545458),transform.rotation);
         }
     }
     if(level >= 10 && level < 15){
         while(totalEnemies < level){
         posX = Random.Range(-4.213592,4.336067);
     
         yield WaitForSeconds(3);
         Instantiate(enemies[1], Vector3(posX, 0.1, 3.545458),transform.rotation);
         }
     }
 }

EDIT GameController.js function Update () { if(!playerspawning){ timer += Time.deltaTime; } //when timer reaches 2 seconds, call Spawn function if(timer >= 2){ Spawn(); }

         if(level == 0 ||cash == level * 200){
                 level++;
                 LevelEnd();
             }
         }
     
     function LevelEnd(){
         //I don't know about the rest of your code, but you have to do 
         //something like this:
         totalEnemies = 0;
     
         EnemyMovement.EnemySpeed += -.1;
     
         if(level <=5){
            while(totalEnemies < level){
            posX = Random.Range(-4.213592,4.336067);
     
            yield WaitForSeconds(3);
            Instantiate(enemies[0], Vector3(posX, 0.1, 3.545458),transform.rotation);
            // Update your totalEnemies variable
            totalEnemies++;
            }
         }
     
         if(level >= 6 && level < 10){
            while(totalEnemies < level){
            posX = Random.Range(-4.213592,4.336067);
     
            yield WaitForSeconds(3);
            Instantiate(enemies[1], Vector3(posX, 0.1, 3.545458),transform.rotation);
            // Update your totalEnemies variable
            totalEnemies++;
            }
         }
     
         if(level >= 10 && level < 15){
            while(totalEnemies < level){
            posX = Random.Range(-4.213592,4.336067);
     
            yield WaitForSeconds(3);
            Instantiate(enemies[1], Vector3(posX, 0.1, 3.545458),transform.rotation);
            // Update your totalEnemies variable
            totalEnemies++;
            }
         }
     }

EnemyMovement.js var enemySpeed:float; static var CoastguardSpeed:float;

 function Awake(){
     enemySpeed = EnemySpeed;
 }
 
 function Update () {
     transform.Translate(0,0,enemySpeed * Time.deltaTime);
     if(transform.position.z < -2.04){
         GameController.totalEnemies -= 1;
         Destroy(gameObject);
     }
 }
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

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by bompi88 · May 08, 2012 at 09:55 PM

You have to update your totalEnemies variable when you instantiates a new enemy. If you don't do this, it keeps looping in the while loop.

 function LevelEnd(){
     //I don't know about the rest of your code, but you have to do 
     //something like this:
     totalEnemies = 0;

     EnemySpeed += -.1;
 
     if(level <=5){
        while(totalEnemies < level){
        posX = Random.Range(-4.213592,4.336067);
 
        yield WaitForSeconds(3);
        Instantiate(enemies[0], Vector3(posX, 0.1, 3.545458),transform.rotation);
        // Update your totalEnemies variable
        totalEnemies++;
        }
     }
 
     if(level >= 6 && level < 10){
        while(totalEnemies < level){
        posX = Random.Range(-4.213592,4.336067);
 
        yield WaitForSeconds(3);
        Instantiate(enemies[1], Vector3(posX, 0.1, 3.545458),transform.rotation);
        // Update your totalEnemies variable
        totalEnemies++;
        }
     }
 
     if(level >= 10 && level < 15){
        while(totalEnemies < level){
        posX = Random.Range(-4.213592,4.336067);
 
        yield WaitForSeconds(3);
        Instantiate(enemies[1], Vector3(posX, 0.1, 3.545458),transform.rotation);
        // Update your totalEnemies variable
        totalEnemies++;
        }
     }
 }

I've edited the code to fit your needs:

     var isSpawningEnemies : boolean = true;
 
     function LevelEnd(){
          // what's the intention of this?
          EnemySpeed += -.1;
     
          while (isSpawningEnemies) {
     
             if(level <=5){
                posX = Random.Range(-4.213592,4.336067);
         
                yield WaitForSeconds(3);
                Instantiate(enemies[0], Vector3(posX, 0.1, 3.545458),transform.rotation);
             }
         
             if(level >= 6 && level < 10){
                posX = Random.Range(-4.213592,4.336067);
         
                yield WaitForSeconds(3);
                Instantiate(enemies[1], Vector3(posX, 0.1, 3.545458),transform.rotation);
             }
         
             if(level >= 10 && level < 15){
                posX = Random.Range(-4.213592,4.336067);
         
                yield WaitForSeconds(3);
                Instantiate(enemies[1], Vector3(posX, 0.1, 3.545458),transform.rotation);
             }
         }
     }

for not to spawn enemies, just:

 isSpawningEnemies = false;
Comment
Add comment · Show 5 · 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 Hedonsoft · May 08, 2012 at 11:29 PM 0
Share

Didn't seem to have any effect at all, but thanks for re$$anonymous$$ding me to add to totalEnemies but after I do this the enemies stop spawning once totalEnemies = level. What's supposed to happen is Waves of enemies will continuously spawn and move toward the player. When they move off screen they are destroyed. The player has to shoot or avoid the enemies while collecting randomly spawning cash. I tried adding GameController.totalEnemies--; to the enemy movement script right before they're destroyed but this didn't do anything. I'm making an edit with more of my code above.

avatar image captaincrunch80 · May 08, 2012 at 11:58 PM 0
Share

The first while loop loops forever! Just as Bjoern said.

Try some debug logs and you will see.

avatar image Hedonsoft · May 09, 2012 at 01:38 AM 0
Share

Ahh yes I see that now. Will work on it tmw and post my solution.

avatar image bompi88 · May 09, 2012 at 11:17 AM 0
Share

You can try my updated code, and see if it behaves properly. Haven't tested it yet.

avatar image Hedonsoft · May 09, 2012 at 02:18 PM 0
Share

That worked great! Thanks. I also see where I went wrong. Thank you very much for explaining it and the example!

BTW EnemySpeed += -.1; increases the speed at which the enemy moves down the screen.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Spawn Multiple Monsters 1 Answer

Spawning enemies if there is no other enemy left. 1 Answer

Can you name something that you spawned with a spawner script? 1 Answer

c# enemy's not spawning 1 Answer

How to spawn more enemies once I've killed one? 1 Answer


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