Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 Stuarttheboy11 · Feb 29, 2020 at 02:15 AM · randomspawningrandomization

How can i rework this script to make enemies spawn at different times and amounts?

I want to be able to make the enemies within the scene spawn randomly at different times and amounts. At the moment my script makes the enemies spawn in the map all at once at random positions. If anyone could help me add to this script and make it so that the enemies would spawn at different amounts and times would be appreciated.

public class RandomEnemySpawn : MonoBehaviour {

 public GameObject Enemies;
 public int xPos;
 public int zPos;
 public int EnemyNumber;

 void Start()
 {
     StartCoroutine(EnemySpawn());
 }

 IEnumerator EnemySpawn()
 {
     while (EnemyNumber < 5) 
     {
         xPos = Random.Range(5, 500);
         zPos = Random.Range(5, 540);
         Instantiate(Enemies, new Vector3(xPos, 1, zPos), Quaternion.identity);
         yield return new WaitForSeconds(0.1f);
         EnemyNumber += 1;
     }

 }

}

Comment
Add comment · Show 1
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 Sylon87 · Feb 29, 2020 at 02:22 AM 0
Share

are you looking to do it recursively?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by joemane22 · Feb 29, 2020 at 03:32 AM

To do this you would need to have two "States" The first is the spawn enemy state, which has a corresponding enemy number to spawn. Then you would have to have a wait to spawn state, which starts at random times. There are a couple of ways to do this but since you are already using coroutines might as well go with that route.


First, you would need an enemy spawn manager. This will run until the end of the game or scene.
Then you will need an enemy spawner, which will only run while the enemies are spawning.
It would look something like this(somewhat a pseudo code, you will need to rework it to get it to work for you)
 public class RandomEnemySpawn: MonoBehaviour
 {
     //is spawning active? this gives you a way to exit the coroutine once its started(always do this in looped coroutines)
     public bool spawningActive;
 
     //this manages spawn times
     IEnumerator EnemeySpawnManager()
     {
         //the loop!
         while(spawningActive)
         {
             //wait a random amount of time before spawning a new set of enemies
             yield return new WaitForSeconds(Random.Range(0.1f, 0.5f));
             //start another coroutine and wait for it to finish(prevents stacking)
             yield return EnemySpawn(Random.Range(1, 5), Random.Range(0.05f, 0.15f));
         }
     }
     //This will spawn a number of enemies with a given wait time between each
     IEnumerator EnemySpawn(int number, float wait)
     {
         for(int i = 0; i < number; i++)
         {
             //your function for spawning an enemy at a random location
             SpawnEnemy();
             yield return new WaitForSeconds(wait);
         }
     }
 }



Don't just copy and past try and figure out how this design works before using it! It is best to actually understand something before using it, this will allow you to adjust it or use a similar pattern later on for a different, but similar problem.
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 Stuarttheboy11 · Feb 29, 2020 at 03:47 AM 0
Share

Thank you a lot! And I agree it is better to understand something before using it, otherwise how do I learn anything!

avatar image joemane22 Stuarttheboy11 · Feb 29, 2020 at 03:59 AM 0
Share

You are welcome! If you are satisfied with the answer please accept it! Good luck to you.

avatar image Stuarttheboy11 joemane22 · Feb 29, 2020 at 05:53 PM 0
Share

I am happy with the answer thank you

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

122 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

,Script only working for one objects and not the others 1 Answer

How to get Buildbox Set Up in Unity 2D 0 Answers

Random AI spawning, random object spawning 1 Answer

Spawning 2D objects randomly and infinitely 1 Answer

How to instantiate prefabs at random times within a time constraint? 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