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 john-essy · Sep 22, 2011 at 09:16 PM · spawnloopenemies

Spawing Enemies works but works to good

The problem i am having is that i only want it to spawn a certain amount of enemies if the amount is met then stop spawning the enemies. But if the enemy count is less than the max Amount start spawning again. Here is my code it works really good but the above is a problem any help would be amazing thanks.

PS. I don't know why this code 101010 button is not working correctly

var AISpawn : Transform[]; var allEnemies : GameObject[]; static var enemiesToSpawn : int; static var currentEnemies : int; var maxEnemiesInt : int; var maxEnemies : boolean = false; var wave1 : boolean = true;

 function Awake()
 {
     if(wave1 == true)
         {    
             Wave1();
         }
 }
 
 function Update()
 {
     if(currentEnemies >= maxEnemiesInt)
         {
             maxEnemies = true;
         }
 }
 
 function Wave1()
 {
     if(maxEnemies == false)
     {
     enemiesToSpawn = 10;
     for(i=0; i < enemiesToSpawn; i++)
         {
             var obj : GameObject = allEnemies[Random.Range(0, allEnemies.length-1)]; // Randomize the different enemies to instantiate.
               var pos: Transform = AISpawn[Random.Range(0, AISpawn.length-1)];  // Randomize the spawnPoints to instantiate enemy at next.
               Instantiate(obj, pos.position, pos.rotation); 
               yield WaitForSeconds(2);
               currentEnemies +=1;
         }
     }
 }

Comment
Add comment · Show 2
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 john-essy · Sep 22, 2011 at 11:04 PM 0
Share

Bumpy!!!!!

avatar image john-essy · Sep 23, 2011 at 05:29 PM 0
Share

Hi Aldonletto one quick Question how would i make this create more enemies if the max enemies has been decreased. I thought maybe it would reset to true if maxenemies was decreased i would really appreciate your help

2 Replies

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

Answer by aldonaletto · Sep 23, 2011 at 01:15 AM

I would use another approach: define minEnemies and maxEnemies, and let the coroutine start spawning enemies until maxEnemies is reached. It then stops, and only restart spawning when the number of enemies fall below minEnemies. For this to work, every enemy must decrement currentEnemies before going to hell, or the coroutine will never know when to restart. You can use Start to do the whole thing, since it's allowed to be a coroutine. The yield WaitForSeconds(2) instruction at the end of the loop gives a 2 seconds pace: each 2 seconds it checks the enemy number and spawns one of them if necessary. Finally: you should use Random.Range(0, array.length), since this function goes to max-1 in the integer version (length-1 makes the last array element never be used)

var AISpawn : Transform[]; var allEnemies : GameObject[]; static var currentEnemies : int = 0; var maxEnemies : int = 15; var minEnemies : int = 10; var spawn = false;

 function Start(){
     while (true){
         if (currentEnemies < minEnemies) 
             spawn = true; // if minEnemies reached start spawning
         if (currentEnemies >= maxEnemies) 
             spawn = false; // if maxEnemies reached stop spawning
         if (spawn){ // if spawning enabled create new enemy
             // Randomize the different enemies to instantiate.
             var obj : GameObject = allEnemies[Random.Range(0, allEnemies.length)];
             // Randomize the spawnPoints to instantiate enemy at next. 
             var pos: Transform = AISpawn[Random.Range(0, AISpawn.length)];  
             Instantiate(obj, pos.position, pos.rotation); 
             currentEnemies +=1;
         }
         yield WaitForSeconds(2); // free Unity for 2 seconds each loop anyway
     }
 }

Comment
Add comment · Show 3 · 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 john-essy · Sep 23, 2011 at 09:19 AM 0
Share

I am going to kiss you if i ever meet you aldonaletto

Thank you so much

avatar image john-essy · Sep 23, 2011 at 09:24 AM 0
Share

There is something i would like to ask you if i may?Can i create an array of functions so that when the player is on a certain wave/Round. It can just call that function Increase the round number and decrease the weapons damage. I know how to most of that but it is just i do not want to have to create 1000's of rounds just one that i can all and increase-decrease things?

avatar image aldonaletto · Sep 23, 2011 at 01:39 PM 0
Share

I think you can declare a function array as var funcs: function[];, initialize and populate it by script, then use it as funcs[n]();
I didn't test this yet - I've just read something about this in an answer from @mohanrao164, which linked to this wiki about delegate: http://www.unifycommunity.com/wiki/index.php?title=$$anonymous$$ain$$anonymous$$enu

avatar image
0

Answer by TheEmeralDreamer · Sep 23, 2011 at 01:02 AM

It seems you need to place a while loop like so inside of the wave function to hold all your other code.
while(playerisinthearea or something like that.) { if(#ofenemiesislessthaniwant) { //create enemies } else { //return to do nothing }

}

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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Creating a looping spawn with an Array 0 Answers

How to create random spawn for an object? (C#) 0 Answers

Increase number of enemies spawned per level 0 Answers

How to spawn enemy? (Enemies) 1 Answer

Using fog/mist to disguise enemy spawning 2 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