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 Infamous911 · Jul 31, 2011 at 10:11 PM · ienumerator

IEnumerator problems in C#

So my problem is that my IEnumerator function won't start. Here is the code I use:

 void Update(){
 Do();
 }
 IEnumerator Do () {
     Debug.Log("HERE");
     yield return new WaitForSeconds(5.0F);
     Instantiate(AIBullet, transform.position, transform.rotation);
 }

I'm not getting the Debug message and nothing is happening.

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

Answer by Peter G · Aug 01, 2011 at 12:02 AM

In C#, you have to use StartCoroutine()

 void Update () {
      StartCoroutine( Do() );
 
      //or the less efficient version that takes a string, but is limited to a single parameter.:
      StartCoroutine("Do" , parameter);
      //The advantage to the string version is that you can call stop coroutine:
      /StopCoroutine("Do");

}

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 Infamous911 · Aug 01, 2011 at 12:35 AM 0
Share

Using the version that takes a string, I'm getting the IEnumerator function to repeat over and over again, but only the message is repeating. So I'm being flooded with the debug message but only once does it wait for 5 seconds then instantiate the object. It seems like it only listens to the last two lines one time.

avatar image Peter G · Aug 01, 2011 at 03:04 AM 0
Share

That's because your calling it from Update(). As Statement is saying, Update will be called every frame even if you start a coroutine. So here's what you time sequence looks like:

 Frame 0: Start Coroutine.
 Frame 1: Start SameCoroutine.
 Frame 2: Start Same Coroutine.
 //....
 5 Seconds + 0 frames: Fire Bullet:
 5 Seconds + 1 Frame: Fire Bullet:

So after that 5 second delay, you will fire every second. In your case it might be easier to use InvokeRepeating:

 void Start () {
      InvokeRepeating( "Do" , 5 , 5 );
 }

or an infinite loop:

  IEnumerator Start () {
      for(;;) {
           Do();
           yield return new WaitForSeconds(5.0f);
      }
  }
avatar image Infamous911 · Aug 01, 2011 at 04:46 AM 0
Share

Thanks, it works now!

avatar image
0

Answer by Statement · Aug 01, 2011 at 02:09 AM

Coroutines won't "hold up" Update. Check out CoUpdate pattern to use logic-blocking calls.

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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Return statement in IEnumerator Unity C# 1 Answer

ISerialization madness 2 Answers

Is Input.GetAxis meant to return to 0 on key up? 1 Answer

Calling IEnumerator function with variables does not do anything 1 Answer

Freeze frame for iTween? 0 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