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 Inan-Evin · Jul 27, 2012 at 10:01 PM · aienemyspawn

Spawning enemies if there is no other enemy left.

Hello everyone. In my game, player needs to defend a point. I spawn 6 enemies at their spawn points per 7 seconds with this code :

 function Start () {
 
 while(true) {
  yield new WaitForSeconds(7);
 Instantiate(s1,spawn1.position,Quaternion.identity);
 Instantiate(s2,spawn2.position,Quaternion.identity);
 Instantiate(s3,spawn3.position,Quaternion.identity);
 Instantiate(s4,spawn4.position,Quaternion.identity);
 Instantiate(s5,spawn5.position,Quaternion.identity);
 Instantiate(s6,spawn6.position,Quaternion.identity);
   }
 }

But what I actually want is, not to spawn them in every 7 seconds. I want to spawn new enemies when the other spawned enemies are dead. I mean, I don't want more than 6 enemies in the scene at the same time. After all of the 6 enemies are dead, I want to spawn 6 more. After they are dead, 6 more and continue like this. I thought some thing like that: when all "enemy" tagged object are null, instantiate the others. But I could not find a way to do it, any ideas ? Thanks :)

Comment
Add comment · Show 5
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 wilco64256 · Jul 27, 2012 at 10:05 PM 1
Share

You could use a counter that goes up when an enemy is spawned and down when an enemy is killed, then just check that counter and make sure it's at zero before allowing the spawn to happen again.

avatar image Inan-Evin · Jul 27, 2012 at 10:15 PM 0
Share

but I can't fit any code into the while(true) , when I tried to put if statement into while(true) statement, Unity has crashed. Also I don't know best way to call instantiated enemies.

avatar image wilco64256 · Jul 27, 2012 at 10:16 PM 1
Share

Yeah I would probably just not do it with the yield statement at all, just run a function to spawn the enemies, then when your counter hits zero run that function again so that this doesn't keep trying to run every seven seconds.

avatar image Inan-Evin · Jul 27, 2012 at 10:26 PM 0
Share

Ok I understand the logic but how am I going to detect spawned enemies. I mean I can use Gameobjectswithtag and make an enemies array by code, but how am I going to know if all the objects in enemies array are null ?

avatar image Seth-Bergman · Jul 27, 2012 at 10:57 PM 0
Share

almost answered your own question:

function Update(){

var enemies = GameObject.FindGameObjectsWithTag("enemy");

if(enemies == null)

Regenerate();

}

but my other answer may be quicker...

1 Reply

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

Answer by Seth-Bergman · Jul 27, 2012 at 10:21 PM

     static var enemyCount : int;
     
     function Regenerate() {
     if(enemyCount <= 0){
     enemyCount = 6;
     Instantiate(s1,spawn1.position,Quaternion.identity);
     Instantiate(s2,spawn2.position,Quaternion.identity);
     Instantiate(s3,spawn3.position,Quaternion.identity);
     Instantiate(s4,spawn4.position,Quaternion.identity);
     Instantiate(s5,spawn5.position,Quaternion.identity);
     Instantiate(s6,spawn6.position,Quaternion.identity);
        }
       }
     
     function Update(){
     if(enemyCount <= 0)
     Regenerate();
     }
 
 then on the enemies script:
 
 var : dead = false;
 
 function Die(){
 if(!dead){
 dead = true;
 RegenScript.enemyCount--;
 
 }
 
 }

and just call Die() when he dies

Comment
Add comment · Show 1 · 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 Inan-Evin · Jul 28, 2012 at 12:00 AM 0
Share

thanks, really good way to do what I wanted. I just could not figure it out, now it works :)

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

7 People are following this question.

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

Related Questions

Need help with my enemy spawn script. 1 Answer

sprite changes z position without any input to do so 1 Answer

Player Damage to enemy damage melee system 0 Answers

Unity AI/Enemy won't move towards target 0 Answers

I would love to know how to keep spawning enemies because when i shoot them they don't spawn. The cause is that the prefab is missing as it says heres my code 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