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 Owen Burk · Jan 03, 2014 at 05:10 PM · enemieswaveshow many

How can I detect the number of enemies?

I'm making a game with waves of enemies. When an enemy dies it sends a message to an empty GameObject saying that it died to keep track of how many enemies are left. How can I determine how many enemies there are (I hoping to do some kind of semi-random spawning).

 #pragma strict
 
 var EnemyCount : ????;
 var waveNumber = 1;
 
 function Died ()
 {
     EnemyCount -= 1;
     if(EnemyCount == 0)
     {
         waveNumber += 1;
         CreateEnemies();
     }
 }
 
 function CreateEnemies ()
 {
     //NotDone
 }
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 roojerry · Jan 03, 2014 at 05:19 PM

 function CreateEnemies ()
 {
     EnemyCount = Random.Range(10,20);
    
     for(var i=0; i < EnemyCount; i++)
     {
         Instantiate(enemyPrefab);
     }
 }

This should be able to get you started. Its up to you to decide how many enemies each wave will have. In this example I just pick a random number between 10 and 20

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 Owen Burk · Jan 03, 2014 at 05:39 PM 0
Share

Thanks You.

avatar image
0

Answer by Kiloblargh · Jan 03, 2014 at 05:22 PM

You were almost there- this should do it:

 #pragma strict
  
 var enemyCount : int;
 var waveNumber : int = 1;
 var enemiesPerWaveMin : int[]; //at least this many per wave (set up in inspector)
 var enemiesPerWaveMax : int[]; //no more than this many per wave
 
  
 function Died ()  {
 enemyCount--;
 if (enemyCount == 0) {
     waveNumber++;
     CreateEnemies();
     }
 }
  
 function CreateEnemies ()  { 
 var howMany : int = Random.Range (enemiesPerWaveMin [waveNumber], enemiesPerWaveMax [waveNumber]);
 for (var e : int = 0; e < howMany; e++)  {

     //spawn one enemy

     enemyCount++;
     }
 }
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 Owen Burk · Jan 03, 2014 at 05:39 PM 0
Share

Thank You. One question, does this make enemiesPerWave$$anonymous$$ax and enemiesPerWave$$anonymous$$in the wave number? how can I change that value. Why is waveNumber in the brackets? It would be good if the enemies gets multiplies by the wave or something along the lines of that.

avatar image Kiloblargh · Jan 03, 2014 at 05:55 PM 0
Share

If you don't know arrays, learn them- Arrays and Lists are key to doing anything useful at all in Unity. An int[] is just several int variables in one variable. You can see in the inspector it has as size and that size is 0. You can set the size to 10 if you want ten waves, then all those slots will open up. 'waveNumber' is being used as the array index (the nth item in each array.)

Suppose you want 4-6 enemies the first wave, 5-10 the second wave, and 12-18 the third wave. So you would fill in enemiesPerWave$$anonymous$$in like

 4
 5
 12
 ...

and enemiesPerWave$$anonymous$$ax like

 7
 11
 19
 ...

If you wanted unlimited waves, you could come up with a function to set the $$anonymous$$ and max, this was just an example to show how you could randomize within a pre set range for each wave.

avatar image Owen Burk · Jan 03, 2014 at 06:54 PM 0
Share

Thanks $$anonymous$$iloblargh and and brianruggieri. :)

avatar image Owen Burk · Jan 03, 2014 at 08:17 PM 0
Share

Awesome, I will have to review arrays, I think I've forgotten most of that stuff. It works well now that I know what to do. Do you know why I got this error?

 IndexOutOfRangeException: Array index is out of range.
 WaveStart+$CreateEnemies$65+$.$$anonymous$$oveNext () (at Assets/Scripts/WaveStart.js:27)
avatar image Kiloblargh · Jan 03, 2014 at 10:41 PM 0
Share

Yes, I do. Every time a wave ends, waveNumber goes up by 1. When waveNumber is greater than the number of entries in enemiesPerWave$$anonymous$$in or enemiesPerWave$$anonymous$$ax; it's going to crash. You need to catch that situation with if (waveNumber >= enemiesPerWave$$anonymous$$in.Length) and then do something keep sending the maximum number of enemies or displaying a "Game over, you won!" message.

Show more comments

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

19 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

Related Questions

Randomize Spawn Times 0 Answers

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

WaveSpawn Script. Help 0 Answers

my wave spawner not working properly 1 Answer

Enemy Select 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