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 Anindya-Nishant · Sep 21, 2016 at 02:16 PM · scripting problemcoroutines

Synchronization in execution of methods of a class using co-routines.

I have a Class which has a method "ExecuteSync". The code snippet of the class containing the function is as follows:

 public class MyTasksClass : Monobehaviour{

     public void ExecuteSync(string actionName){
 
 switch(actionName){
     
     case "action1" :
             StartCoroutine(ActionOne());
         
     break;
     
     case "action2" :
         StartCoroutine(ActionTwo());
     break;
     
 }
 
 }

 
 IEnumerator ActionOne(){
     Debug.Log("Action One Started");
     int a = 0;
     while(a<100){
         yield return null;
     }
     Debug.Log("Action One Ended");
     yield return null;
 }
 
 //Please note that these two methods are not related to one another.
 IEnumerator ActionTwo(){
     Debug.Log("Action Two Started");
     int a = 500;
     while(a<1000){
         yield return null;
     }
     Debug.Log("Action Two Ended");
     yield return null;
 }
 
 }


There is another class UIController from which we can call these methods like as follows:

 public class UIController : Monobehaviour{

    public MyTasksClass myTaskClass;

    void Start(){
     myTaskClass = FindObjectOfType<MyTasksClass>();
     
     Debug.Log("Synchronous started");
     myTaskClass.ExecuteSync(action1);
     Debug.Log("Synchronous ended");
    }

  }


The expected output should print in this order:

 Synchronous started
 Action One Started
 Action One Ended
 Synchronous ended


But the actual output is as follows:

 Synchronous started
 Synchronous ended
 Action One Started
 Action One Ended

Is there any way to make it work in a Synchronous way to print the expected output ? Any help will be appreciated.

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
0

Answer by Leoo · Sep 21, 2016 at 03:08 PM

Well that's exactly how people would expect Coroutines to work.

Coroutines wont just stop your "Start" method for the frames they need to finish, that's just not how it works.

I would recommend to just keep all that logic in a separate function that handles what happens "after" the coroutine end, stay out of the "start" method.

  IEnumerator ActionOne(){
      Debug.Log("Action One Started");
      int a = 0;
      while(a<100){
          yield return null;
      }
     CoroutineEnded("Action One Ended");
      yield return null;
  }
  
  //Please note that these two methods are not related to one another.
  IEnumerator ActionTwo(){
      Debug.Log("Action Two Started");
      int a = 500;
      while(a<1000){
          yield return null;
      }
      CoroutineEnded("Action Two Ended");
      yield return null;
  }

 public void CoroutineEnded(string msg){
     Debug.Log(msg);
 }



  

https://docs.unity3d.com/Manual/Coroutines.html

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 Anindya-Nishant · Sep 22, 2016 at 06:45 AM 0
Share

Hi @Leoo Thank you for the prompt response.

What we want to achieve is that the log "Synchronous ended" should get printed only when the entire execution in "ExecuteSync" function gets completed.

avatar image Anindya-Nishant · Sep 23, 2016 at 06:42 PM 0
Share

Any suggestions please?

avatar image Leoo Anindya-Nishant · Sep 23, 2016 at 07:43 PM 0
Share

There you go, look at the edit.

avatar image Anindya-Nishant Leoo · Sep 24, 2016 at 10:42 AM 0
Share

Hi @Leoo that is exactly the same thing that happened previously. There is not much of any difference in the code execution.

I want to block the execution of Debug.Log("Synchronous ended"); unless and until myTaskClass.ExecuteSync(action1); gets completed.

and we cannot have the return type of 'ExecuteSync' as IEnumerator.

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

Climbing a block in 2d 0 Answers

How to run the Coroutine of one script in another using the onMouseDown function ? 2 Answers

Multiple coroutines starting from Update() freeze/crash Unity 0 Answers

Coroutine not working properly 1 Answer

Can you run a coroutine more often than Update? 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