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 Carson365 · Dec 31, 2017 at 10:03 PM · coroutinespawnienumeratorobstacle

How to have IEnumerators run but not in Update

Hello! I am making a game where when the game has ran for a certain amount of time, the obstacles spawn faster. I make them spawn by using IEnumerator to where when an obstacle spawns every 3-ish seconds. I tried to stop and start a coroutine in an if statement in void Update, but when it got called, instead of the objects spawning every 3-ish seconds, the obstacles just spawned every frame. Anyone know how I can fix this? Thanks.

Code:

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class Spawner : MonoBehaviour {

 public GameObject obstacle;
  
 Vector3 obstacleSpawnPos = new Vector3 (0.0f, 0.0f, 0.0f);

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

 void Update () {
     if (Time.time > 15) {
         StopCoroutine (FirstObstacleSpawnRoutine());
         StartCoroutine (SecondObstacleSpawnRoutine());
     }

     if (Time.time > 30) {
         StopCoroutine (SecondObstacleSpawnRoutine());
         StartCoroutine (ThirdObstacleSpawnRoutine());
     }

     if (Time.time > 50) {
         StopCoroutine (ThirdObstacleSpawnRoutine());
         StartCoroutine (FourthObstacleSpawnRoutine());
     }
 }

 IEnumerator FirstObstacleSpawnRoutine () {
     while (true) {
         Instantiate (obstacle, obstacleSpawnPos, Quaternion.identity);
         yield return new WaitForSeconds (Random.Range (3.0f, 5.0f));
     }
 }

 IEnumerator SecondObstacleSpawnRoutine () {
     while (true) {
         Instantiate (obstacle, obstacleSpawnPos, Quaternion.identity);
         yield return new WaitForSeconds (Random.Range(2.5f, 4.25f));
     }
 }

 IEnumerator ThirdObstacleSpawnRoutine () {
     while (true) {
         Instantiate (obstacle, obstacleSpawnPos, Quaternion.identity);
         yield return new WaitForSeconds (Random.Range(2.25f, 3.5f));
     }
 }

 IEnumerator FourthObstacleSpawnRoutine () {
     while (true) {
         Instantiate (obstacle, obstacleSpawnPos, Quaternion.identity);
         yield return new WaitForSeconds (Random.Range(2.0f, 3.0f));
     }
 }

}

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
1

Answer by Lysander · Dec 31, 2017 at 10:23 PM

Just make a new coroutine called SpawnObjects or something, and in there start the first wave, then yield wait 15 seconds, then stop the first and start the second, then yield wait for 15 more seconds, then stop the second and start the third, etc... Start the primary coroutine in Start, or via a method, and not in Update. You can delete the Update method entirely.

Honestly though, in this case, you can get away with one or two coroutines. You can have the ObstacleSpawnRoutine just take two parameters for the time to wait between spawns (min and max to use in the Range function), and just stop and start the same routine with different parameters set. Even better would be to use private fields or properties to get those values, and start two coroutines- the first just keep track of the timing of the waves and sets those fields, while the second ObstacleSpawnRoutine just uses the values to spawn objects with that timing.

 public GameObject obstacle;
 public Vector3 obstacleSpawnPos = Vector3.Zero;
 
 private float minSpawnTime = 3f;
 private float maxSpawnTime = 5f;
 
 private WaitForSeconds waveTimer;
 

 private void Awake()
 {
     waveTimer = new WaitForSeconds(15f);
     StartCoroutine("WaveRoutine");
     StartCoroutine("SpawnRoutine");
 }
 
 private IEnumerator WaveRoutine()
 {
     minSpawnTime = 3.0f;
     maxSpawnTime = 5.0f;
     yield return waveTimer;
 
     minSpawnTime = 2.5f;
     maxSpawnTime = 4.5f;
     yield return waveTimer;
 
     minSpawnTime = 2.0f;
     maxSpawnTime = 4.0f;
     yield return waveTimer;
     
     // etc...
 }
 
 private IEnumerator SpawnRoutine()
 {
     while(true)
     {
         Instantiate(obstacle, obstacleSpawnPos, Quaternion.identitiy;
         yield return new WaitForSeconds(Random.Range(minSpawnTime, maxSpawnTime));
     }
 }

If you want to store the spawn timings in a collection of float/float objects, you can just iterate through that collection for the WaveRoutine until it runs out of items as well, so it's not hardcoded in the routine.

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

Answer by Carson365 · Dec 31, 2017 at 10:31 PM

Haha. never thought about that. Works perfectly now. Thanks!

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 Lysander · Dec 31, 2017 at 10:45 PM 0
Share

Please be sure to make comments rather than answers when responding to someone on here- there's an option for "convert to comment" above your post you can use to fix that if you hit the wrong button. Also, if that answered the question, please mark it as accepted (an upvote wouldn't hurt either ^_~).

And you're welcome ^_^

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

77 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

Related Questions

spawn timer problem 2 Answers

Spawn objects in burst of three? 1 Answer

Enemy spawning problem... 0 Answers

Can I use StartCoroutine inside of the same coroutine with a new variable? 0 Answers

How to stop coroutines, when paused 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