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 /
  • Help Room /
avatar image
0
Question by Noxidl · May 21, 2017 at 03:12 AM · c#coroutine

Coroutine not waiting for a set amount of time!

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

public class TestCOntorller : MonoBehaviour { public GameObject x; private float z,c; void Start () {

 }
 
 // Update is called once per frame
 void Update () {
     StartCoroutine (kocence ());

 }
 IEnumerator kocence(){Debug.Log ("dadD");
     z = Random.Range (1f, 2f);



     if (z <= 1.5) {
         c = 0.3f;
     } else {
         c = 1.8f;
     }
     Instantiate (x, new Vector3 (2.93f, c, 0f),Quaternion.identity);
     yield return new WaitForSeconds(10);
     //Debug.Log ("dadD");
 }}

My goal is to wait after every instantiate and not spam the screen with GameObjects .The Debug.Log does appear when i run the game "Debug.Log ("dadD");" so the problem should be in the yield or at least i think so . I have no expiriance with coroutins thus any help willl be appriciated.

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
1
Best Answer

Answer by jdean300 · May 21, 2017 at 03:49 AM

The problem is that you are starting a new coroutine in every update. What you want to do is this:

 public bool spawning = false;
 
 private void Start()
 {
    StartSpawning();
 }
 
 public void StartSpawning()
 {
     spawning = true;
     StartCoroutine(kocence())
 }
 
 public void StopSpawning()
 {
     spawning = false;
 }
 
 IEnumerator kocence(){
     while (spawning) {
         Debug.Log ("dadD");
         z = Random.Range (1f, 2f);
         if (z <= 1.5) {
             c = 0.3f;
         } else {
             c = 1.8f;
         }
         Instantiate (x, new Vector3 (2.93f, c, 0f),Quaternion.identity);
         yield return new WaitForSeconds(10);
     }
  }
Comment
Add comment · Show 2 · 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 Pangamini · May 21, 2017 at 09:07 AM 1
Share

Careful, this solution could lead into multiple coroutines spawning in parallel, if you use StopSpawning and then StartSpawning before the coroutine gets to the while(spawning) check. Ins$$anonymous$$d, save the Coroutine handle returned by StartCoroutine and use that with StopCoroutine call.

avatar image dustin_pau1 · Jul 15, 2017 at 05:17 PM 0
Share

Thank you! I was going insane trying to figure out why my coroutine wasn't waiting. It was because the Update() method was creating new coroutines each frame. The individual coroutines were waiting, but it seemed like they weren't since so many of them had instantiated. To fix this I created a boolean called "waiting" and set it to true before the waitForSeconds and set it false after I completed my delay. And I only called the coroutine if "waiting" was false.

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

9 People are following this question.

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

Related Questions

How to get a for loop to run once per coroutine? 0 Answers

Coroutine and SetFloat not working as expected 0 Answers

Coroutine Not Starting - Crashes Instead? 1 Answer

Trying to move buttons via Coroutine and transform.Translate 0 Answers

How to get a for loop to run once per coroutine? 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