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.