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 Gentatsu · May 08, 2017 at 08:49 AM · animationcoroutinepositioningrecursion

Running coroutines in a recursive function. How to wait until all coroutines done before going to next level

I've got a coroutine which moves the position of several gameobjects in a hierarchy. The general structure is like this:

 void someFunction()
 {
     ...
     if (value)
         someFunction()
     ...
     Func1()
 }
 
 void Func1()
 {
     StartCoroutine(Func2(root))
 }

 IEnumerator Func2(Node node)
 {
      StartCoroutine(Func2(node.child))
 }

It's a little more nested than that, and there's a couple of coroutines in there. One is recursive, and one runs on the parent of the current root (if it has one).

I know it's fairly specific, but in essence, I'd like to wait after the someFunction call inside itself before running any further. I do this to move up an down a tree and traverse it one by one instead of straight to it. Any ideas?

Comment
Add comment · Show 4
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 SohailBukhari · May 08, 2017 at 09:35 AM -1
Share

To get the end moment of the coroutine you can make the coroutine call a function or send an event in its last line.

The function can be called using delegates or by harcoding it (calling the name of the function itself). If you just want to check for the end of the coroutine I suggest you make the coroutine send an event. (and the event handler can do whatever you need)

Just use a bool like this: bool running;

 void Invoke$$anonymous$$yCoroutine()
  {
       StartCoroutine("Coroutine");
  }
  
  IEnumerator Coroutine()
  {
     running= true;
     //do Stuff
     yield return //Whatever you want
     running= false;
  }
  
  // As long as you a within this scope you can just do :
  
  void AnotherFunction()
  {
     if (running)
     {
         // Do some other stuff;
     }
  
  }

avatar image Gentatsu · May 08, 2017 at 09:47 AM 0
Share

I did try this, but because I'm running several, I tried making a list of IEnumerators and then loop through each one, waiting for them to finish and yield them, then at the end set a bool to say they're all finished, and then I stuck a while (coroutinesRunning) after it, but alas it just hung.

avatar image SohailBukhari Gentatsu · May 08, 2017 at 09:58 AM 0
Share

post your code

avatar image Gentatsu SohailBukhari · May 08, 2017 at 10:01 AM 0
Share

There's -a lot- of code. The above is a $$anonymous$$WE of the problem.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by m-coto · May 18, 2017 at 11:02 AM

Your someFunction should be an IEnummerator and you should wait for the inner coroutines to be finished using something like this:

     yield return StartCoroutine(Func2(root));

That should only continue to run code when the coroutine it's calling has finished.

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

157 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 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

Coroutine is not working 2 Answers

Coroutine mystery (for a noob) 1 Answer

Can I make animations snap to a frame? 1 Answer

Animation Event issue what am I doing wrong here 2 Answers

Detecting last frame of animation, without StateMachineBehaviours or Animation Events 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