Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
1
Question by Leoss · Jun 05, 2013 at 10:00 AM · c#coroutineyieldwithoutno

Using yield without StartCoroutine() in C#

Hi, I would like to have a function that do difference things each time I call it, like the following:

     IEnumerator DoNextAction()
     {
         Debug.LogWarning("First");
         yield return null;
         Debug.LogWarning("Second");
         yield return null;
         Debug.LogWarning("Third");
         yield return null;
         Debug.LogWarning("Finish !");
     }
         
     void Update()
     {
         if (Input.GetMouseButtonDown(0))
         {
             DoNextAction();
         }
     }

I want that each time I click with my mouse, DoNextAction() is called and execute instructions until the yield return is called. That is normally how 'foreach iterators' work. (The first click should print "First", then if I click again "Second", etc...) The problem is that each time I click, DoNextAction does nothing.

And I don't want to use StartCoroutine(), because if I do:

 void Update()
     {
         if (Input.GetMouseButtonDown(0))
         {
             StartCoroutine(DoNextAction());
         }
     }

StartCoroutine will do all the yields (each time click, it would print "First", "Second", "Third" and "Finish" all at once...)

What is the solution ?

Thanks.

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

3 Replies

· Add your reply
  • Sort: 
avatar image
4
Best Answer

Answer by TonyLi · Jun 05, 2013 at 07:55 PM

Try something like this:

 IEnumerator<bool> actionEnumerator;
         
 IEnumerable<bool> ActionIterator() {
     Debug.LogWarning("First");
     yield return true;
     Debug.LogWarning("Second");
     yield return true;
     Debug.LogWarning("Third");
     yield return true;
     Debug.LogWarning("Finish !");
     yield return false;
 }
 
 void Start() {
     actionEnumerator = ActionIterator().GetEnumerator();
 }
 
 void Update() {
     if (Input.GetMouseButtonDown(0)) {
         actionEnumerator.MoveNext();
     }
 }
Comment
Add comment · Show 4 · 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 TonyLi · Jun 06, 2013 at 03:51 AM 0
Share

I just threw this into a project and confirmed that it works fine. You'll need to include these namespaces:

 using UnityEngine;
 using System.Collections.Generic;
avatar image Leoss · Jun 07, 2013 at 12:13 PM 0
Share

Thanks man ! That is perfectly working.

avatar image almo · Aug 04, 2014 at 10:41 PM 0
Share

Can someone tell me what the return true and return false do?

avatar image Bunny83 · Aug 04, 2014 at 10:49 PM 0
Share

@almo: It does nothing in this case. I don't understand why he actually used a typed IEnumerator / IEnumerable. Since the yielded value is pointless here it would be much easier to just use IEnumerator. $$anonymous$$oveNext returns true if there is more code to go and naturally return false if the iteration is done.

avatar image
0

Answer by Julianobsg · Jun 06, 2013 at 01:59 AM

First off all, you can´t... following unity reference:

When using JavaScript it is not necessary to use StartCoroutine, the compiler will do this for you. When writing C# code you must call StartCoroutine.

So to use yield you need a StartCoroutine, your actual problem is the idea of yield. The yield will continue in the next update, although the function is called or no.

What I suggest to do is not using yield, try to do a case function with a tick counter? Maybe every time you click it´s add a counter and for each click it does that function. That make more sense for me.

If you want to use yield anyway. You should know that wield wait for a function to complete, so for example, wait for seconds will expend X seconds to end, and the function will go after that ends.

Sorry about the english, or seemed rude, not my native language...

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 TonyLi · Jun 06, 2013 at 03:50 AM 1
Share

A coroutine is just a type of enumerator. You can use yield with any enumerator, not just coroutines.

avatar image Julianobsg · Jun 06, 2013 at 11:26 AM 0
Share

that never been clear for me, and make sense, since the idea of using yield is to avoid status managing...

avatar image
0

Answer by codestage · Aug 04, 2014 at 11:37 PM

Did you knew you may yield right from Unity's callbacks? Just like this:

         private IEnumerator Start()
         {
             yield return new WaitForSeconds(5);
             Debug.Log("Hey, 5 secs are passed by already!");
         }

Same for Awake, Update and other callbacks AFAIR.

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 Bunny83 · Aug 05, 2014 at 12:15 AM 0
Share

It is true that some callbacks can be coroutines, but especially Awake and Update can't. It's stated for each callback in the documentation which can and which can't be coroutines.

Also the OP don't wanted to use Unity's coroutine system at all but simply use an IEnumerator as normal iterator.

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

17 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

Related Questions

What am I doing wrong with IEnumerator? 1 Answer

How To cycle coroutine in C# say every 1 sec? 2 Answers

Playing footsteps with interval 1 Answer

[C#]Spawnmanager loop 2 Answers

In coroutin function,if(x) { yield return a } . if(y) { yield return b }. Is it possible? 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