• 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
Question by letmeknowit · Feb 26, 2012 at 03:29 AM · c#yieldwaitforsecondsstartcoroutine

Another question about yield, WaitForSeconds in c#

Hi

I was reading toons of posts regarding WaitForSeconds in c# and cant get it working. "WaitForSeconds" just don`t run like not beeing in code.

Any clues ????

 using UnityEngine;
 using System.Collections;
 using System;
 
 public class MyCreator : MonoBehaviour {
     
     
     public GameObject box;
     public bool readynow = true;    
     int counter =0;
     public int limit=100;
     
     // Update is called once per frame
     void Update () {
         
         if(counter<=limit) {
         StartCoroutine(MakeBox());
             counter++;
     }
 }
        IEnumerator MakeBox() {
         readynow=false;    
         Instantiate(box, transform.position, transform.rotation);
         
         yield return new WaitForSeconds(4.0f);
         readynow=true;
     }
 }
Comment

People who like this

0 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 Eric5h5 · Feb 26, 2012 at 03:32 AM 2
Share

For one thing, you're starting a new instance of the coroutine every frame for the first 101 frames.

2 Replies

· Add your reply
  • Sort: 
avatar image

Answer by syclamoth · Feb 26, 2012 at 03:37 AM

What do you expect this code to do? Right now, I would expect it to run like this:

once per frame, start the box coroutine, until the counter reaches 101.

In this box coroutine, it spawns a box immediately and switches a boolean (that isn't used anywhere else in the code).

4 seconds later, the boolean gets switched back. Again, since this bool isn't used anywhere else in the code, I would expect it to do nothing. You may as well just remove the coroutine entirely, since the way you are using it now is identical to simply instantiating a box once per frame (except with a little more overhead).

If, as I suspect, you actually want it to instantiate a box every 4 seconds, you should instead structure it a bit more like this:

IEnumerator MakeBoxes(int boxCount, float boxDelay) {

 for(int i = 0; i &lt; boxCount; ++i)
 {
     Instantiate(box, transform.position, transform.rotation);
     yield return new WaitForSeconds(boxDelay);
 }

}

Then, use StartCoroutine once from Start, and let the coroutine manage the timing.

Otherwise, what will happen is the 101 boxes will spawn, then 4 seconds after the first one appears, the boolean will reset. After this, it will be impossible to change the boolean permanently for about 101 frames, and then the coroutines will have finished.

Comment
Eric5h5
Bunny83

People who like this

2 Show 0 · 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

Answer by letmeknowit · Feb 26, 2012 at 04:10 PM

Well syclamoth u make me think about this issue more and i found solution to this problem. The code u showed me is nice and simple and i like it.

My question/issue was made by trying make this tutorial lesson tutorial lesson -pausing scripts- parsed as c# script. But to the point, i found information that Coroutines can`t be started from update() function .. thats why this wasn`t working. Thanks for pointing me in solution direction.

regards letmeknowit

Comment

People who like this

0 Show 0 · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

6 People are following this question.

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

Related Questions

C# WaitForSeconds doesn't seem to work ?? 2 Answers

Playing footsteps with interval 1 Answer

WaitForSeconds problem with Unity Pro 3 Answers

Mysteries of yield 1 Answer

WaitForSeconds not running 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