Search Unity

Coroutines won't get called the second time when it's added outside the class

Discussion in 'Scripting' started by Ali_Akbar_Montazeri, Aug 24, 2019.

  1. Ali_Akbar_Montazeri

    Ali_Akbar_Montazeri

    Joined:
    Dec 6, 2014
    Posts:
    184
    So this is my first time I encountered a strange behaviour from the coroutine. I have a list of coroutine that will get called after I hit a button. To be clear I want to make something like this. The coroutines succesfully did their job if I populate them inside the class but they won't get called the second time if I populate them outside the class.
    The
    Compiler.cs
    class contains function that do compiling things:
    Code (CSharp):
    1.  
    2. // Populate the command
    3. public List<IEnumerator> action = new List<IEnumerator>();
    4.  
    5.     private void Awake()
    6.     {
    7.         I = this;
    8.     }
    9.  
    10.     private void Start()
    11.     {
    12.         // First index blank command
    13.         action.Add(CommandInstantiator.I.parent.GetChild(0).GetComponent<Command>().command);
    14.         //StartCoroutine(Compile());
    15.     }
    16.  
    17.     // Read
    18.     void ReadInput(InputType input) // Let's populate them in this class
    19.     {
    20.         // Pick action
    21.         switch (input)
    22.         {
    23.             case InputType.WALK:
    24.                 action.Add(Action.I.Walk());
    25.                 break;
    26.             case InputType.ROTATE_L:
    27.                 action.Add(Action.I.RotateL());
    28.                 break;
    29.             case InputType.ROTATE_R:
    30.                 action.Add(Action.I.RotateR());
    31.                 break;
    32.             case InputType.LOOP:
    33.                 //action.Add(Action.I.Loop(action));
    34.                 break;
    35.             default:
    36.                 break;
    37.         }
    38.     }
    39.  
    40.     // Populate
    41.     void Populate()
    42.     {
    43.         for (int i = 0; i < input.Length; i++)
    44.         {
    45.             ReadInput(input[i]);
    46.         }
    47.     }
    48.  
    49.     public void DoCompile()
    50.     {
    51.         StartCoroutine(Compile());
    52.     }
    53.  
    54.     // Compile
    55.     IEnumerator Compile()
    56.     {
    57.         Populate();
    58.  
    59.         // Start at first index because we have penguin command
    60.         for (int i = 1; i < action.Count; i++)
    61.         {
    62.             print("running");
    63.             yield return StartCoroutine(action[i]);
    64.             print("command at: " + i + " is: " + action[i]);
    65.         }
    66.  
    67.         print("finished");
    68.     }
    But then I'm extending the compiler function. Whenever I dropped a command to the command box, it will add a coroutine to the
    List<IEnumerator> action
    and it should compile after I hit a compile button. The script for adding a command is below:
    Code (CSharp):
    1.  
    2. // Drag function
    3. //.....
    4. // If the mouse is released
    5. if (Input.GetMouseButtonUp(0))
    6.         {
    7.             if (objectToDrag != null)
    8.             {
    9.                 var UIobject = GetObjectUnderMouse(); // Contains object to replace
    10.  
    11.                 if (UIobject != null)
    12.                 {
    13.                     //objectToDrag.position = objectToReplace.position;
    14.                     //objectToReplace.position = originalPosition;
    15.  
    16.                     if (UIobject.tag == ActionTag.UIContainer.ToString())
    17.                     {
    18.                         // Spawn command
    19.                         Command com = objectToDrag.GetComponent<Command>();
    20.                         CommandInstantiator.I.SpawnCommand(com.comType.ToString());
    21.                         // Let's add a command from outside the Compiler.cs class
    22.                         Compiler.I.action.Add(com.command);
    23.                     }
    24.                     else if (UIobject.tag == ActionTag.UIDraggable.ToString())
    25.                     {
    26.                         // Replace
    27.                     }
    28.                     else
    29.                     {
    30.                         print(UIobject.tag);
    31.                     }
    32.  
    33.                     // If mouse released when no object overlapped, return to the original position
    34.                     objectToDrag.position = originalPosition;
    35.                 }
    36.                 else
    37.                 {
    38.                     // If mouse released when no object overlapped, return to the original position
    39.                     objectToDrag.position = originalPosition;
    40.                 }
    41.  
    42.                 objectToDragImage.raycastTarget = true;
    43.                 objectToDrag = null;
    44.             }
    45.  
    46.             dragging = false;
    47.         }
    I hope my explanation is clear. Any help will be appreciated. Thank you!
     
  2. Ali_Akbar_Montazeri

    Ali_Akbar_Montazeri

    Joined:
    Dec 6, 2014
    Posts:
    184
    Since no one can't give some help about this strange coroutine behaviour, I've decided to make some key to match the coroutine list and called it inside the class