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 tophe_981 · Jan 13, 2020 at 10:38 AM · coroutinespawningwaitforseconds

How to start another Coroutine after a specific condition is met while another Coroutine is running?

I'm creating a defense game wherein different types of enemies would rush towards the player's base. I would like to design it as if there would be newer enemies as the player achieves a specific amount of score. So far, I made a Coroutine for the first enemy type (easysphere) to spawn. But then, I would like a new enemy type (mediumsphere) to spawn after the player achieves a score of 30. However, when the score hits 30, there are just too much instantiated mediumspheres as if it fills up the entire game view. I just wanted it to spawn normally within the interval of 4 seconds.

Here's my code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class SpawnController : MonoBehaviour
 {
     public GameObject easysphere, mediumsphere;
     // Start is called before the first frame update
     void Start()
     {
         StartCoroutine(SpawnEnemy());
     }
 
     // Update is called once per frame
     void Update()
     {
         if (PlayerController.scores == 30)
         {
             StartCoroutine(SpawnMediumSphere());
         }
     }
     IEnumerator SpawnEnemy()
     {
         while (true)
         {
             float x = Random.Range(-360f, 360f);
             float z = Random.Range(-300f, -270f);
 
             GameObject a = Instantiate(easysphere);
             a.transform.position = new Vector3(x, 0.5f, z);
 
             yield return new WaitForSeconds(2f);
         }
     }
     IEnumerator SpawnMediumSphere()
     {
         while (true)
         {
             float x = Random.Range(-360f, 360f);
             float z = Random.Range(-270f, -240f);
 
             GameObject b = Instantiate(mediumsphere);
             b.transform.position = new Vector3(x, 0.5f, z);
 
             yield return new WaitForSeconds(4f);
         }
     }
 }
 





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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by jleemans · Jan 13, 2020 at 12:01 PM

Your "StartCoroutine(SpawnMediumSphere());" is in Update, it means it will be called every frame while the player has a score of 30. So it will start multiple coroutines. You need to launch it only one time. Maybe try will a bool that tell if you have already start the medium coroutine

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 tophe_981 · Jan 13, 2020 at 01:41 PM 0
Share

I tried adding a bool. It spawns perfectly at first, with the interval of 4 seconds. But at the second spawning the mediumspheres spawn twice at the same time.

avatar image jleemans · Jan 13, 2020 at 01:52 PM 0
Share

Your code should be like this, no ? When player reach 30, it will spawn meium every 4 sec, and easy every 2

  using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  
  public class SpawnController : $$anonymous$$onoBehaviour
  {
      public GameObject easysphere, mediumsphere;
      privat bool mediumStarted = false;
      
      // Start is called before the first frame update
      void Start()
      {
          StartCoroutine(SpawnEnemy());
      }
  
      // Update is called once per frame
      void Update()
      {
          if (PlayerController.scores == 30 && !mediumStarted)
          {
              mediumStarted = true;
              StartCoroutine(Spawn$$anonymous$$ediumSphere());
          }
      }
      IEnumerator SpawnEnemy()
      {
          while (true)
          {
              // Spawns easy 
              yield return new WaitForSeconds(2f);
          }
      }
      IEnumerator Spawn$$anonymous$$ediumSphere()
      {
          while (true)
          {
              // Spawns medium 
              yield return new WaitForSeconds(4f);
          }
      }
  }
avatar image tophe_981 jleemans · Jan 13, 2020 at 02:08 PM 0
Share

I never knew it could be as simple as that. It was because I extended my bools to the mediumsphere IEnumerator that's why it happened. But thanks a lot it works now!

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

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

Related Questions

How to make a spawner wait for x seconds? 2 Answers

Coroutine confusion 1 Answer

Getting an error when trying to yield 2 Answers

Corountines and messaging system 0 Answers

WaitForSeconds Not Working 4 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