title: Creating Coroutine for Listeners
body: <p>My main script sorts a set of balls 1 by 1, I am calling that script into this so when I <a href="https://i.stack.imgur.com/GALfG.png" rel="nofollow noreferrer">click this</a> button it will trigger sorting button and it will sort 1 by 1 by waiting in example 1-2 sec between each swapping. But when I click the button that made for triggering an automated sort in sorts immediately so in here Coroutine doesnt do what it suppose to do.</p>
  
  <p>This is unsorted set of balls. <a href="https://i.stack.imgur.com/849bj.png" rel="nofollow noreferrer">Here</a>
  Is there any way to fix this?  </p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using UnityEngine;
  using UnityEngine.UI;
  using System.Collections;
  
  
  namespace Assets
  {
      public class Trigger : MonoBehaviour
      {
          public Gameobjects _Gameobjects; 
          public Button e_YourButton;
          public void Start()
          {
              Button btn = e_YourButton.GetComponent&lt;Button&gt;();
              for (int k = 0; k &lt; 4; k++)
              {
                  btn.onClick.AddListener(_Gameobjects.TaskOnClick);
                  btn.onClick.AddListener(() =&gt; { _Gameobjects.Click1(); });
                  StartCoroutine(Example());    
              }
          }   
  
          IEnumerator Example()
          {
  
             yield return new WaitForSeconds(1);
          }
      }
  }
  </code></pre>
  
  <p>Main script;</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using UnityEngine;
  using UnityEngine.UI;
  using System.Collections;
  
  
  namespace Assets
  {
      public class Gameobjects : MonoBehaviour
      {
          public Button s_YourButton;
          [SerializeField]
  
          public GameObject[] Balls = new GameObject[5];
  
          public bool Click = false;
  
          private static int i = 0;
          private int j = i + 1;
          private int increasebyclick = 1;
  
          private Vector3 posA = Vector3.zero; //Vector3.zero is for initialization
          private Vector3 posB = Vector3.zero; //Vector3.zero is for initialization
  
  
          public GameObject[] instantiatedObjects= new GameObject[5];
  
          void Start()
          {
              Button btn = s_YourButton.GetComponent&lt;Button&gt;();
              //Calls the TaskOnClick method when you click the Button
              btn.onClick.AddListener(TaskOnClick);
              btn.onClick.AddListener(() =&gt; { Click1(); });
              Balls[0] = GameObject.Find("5");
              Balls[1] = GameObject.Find("3");
              Balls[2] = GameObject.Find("2");
              Balls[3] = GameObject.Find("4");
              Balls[4] = GameObject.Find("1");
          }
  
  
          public void TaskOnClick()
          {
  
              performInsertionSort(Balls);
  
  
  
          }
          public void Click1()
          {
              i += increasebyclick;
              print(i);
              if (i &gt;= 4)
              {
                  i = 0;            }
          }
  
  
          private void performInsertionSort(GameObject[] Balls)
          {
              {
  
  
  
                          if (string.Compare(Balls[i].name, Balls[i+1].name) &gt; 0)
                          {
                              GameObject temp = Balls[i];
                              Balls[i] = Balls[i+1];
                              Balls[i+1] = temp;
  
                              posA = Balls[i].gameObject.transform.position;
                              posB = Balls[i + 1].gameObject.transform.position;
                              Balls[i].gameObject.transform.position = posB;
                              Balls[i + 1].gameObject.transform.position = posA;
  
  
                              }
                  }
              }       
          }
      }
  </code></pre>