OnClick to trigger function inside Update only once & using Vector3.MoveTowards inside that function call

Hi, I am not exactly sure the best way to proceed with my code.

I have something like this (C#) - JavaScript solutions also accepted:


public List<GameObject> prefabs = new List<GameObject>(); //Where 5 prefabs are added to the list via the Inspector
private List<GameObject> objects = new List<GameObject>();

public int num1 = 10, num2 = 5;

void Awake() {
    for(int i = 0; i <  num1; i++) {
        for(int j = 0; j < num2; j++) {
            objects.Add((GameObject) Instantiate(prefabs[j], Vector3(i, j, 0), Quaternion.identity));
        }
    }
}

public void Update() {
    myFunction();
}

public void myFunction() {
    int num = Random.Range(1, num1*num2);
    for(int i = 0; i < num; i++) {
        Vector3 currentX = objects*.transform.position.x;*

objects.transform.position = Vector3.MoveTowards(objects.transform.position, Vector3(currentX +1, objects_.transform.position.y, objects*.transform.position.z), Time.deltaTime);
}
}


*
This code creates 10 (num1) instances of 5 (num2) prefabs and places each object in a list to be easily accessed later. The myFunction() function then gets a random number based on num1 & num2 (50 in this case) and moves that many prefabs in the x direction by 1._

I am having issues linking the above code to an OnClick() function. The OnClick() function (from a UI button) should call the myFunction() function from this script (attached to a separate object) when it is clicked. From there, the objects should move in the specified direction until they’ve reached their destination.

If I try the current code, all the prefabs jitter in space. Attaching it to the Onclick function makes the objects move 1 step and then the function ends.

How am I to incorporate Vector3.MoveToward() inside the for loop with the Update function (to make it move) as well as allowing the function to be triggered by an OnClick event?
Solutions using something else in the place of Vector3.MoveToward are accepted as well.
To sum it up: I am looking to move my objects from within the for loop (or somehow gaining access to specific items outside the for loop) in the Update function (using Time.deltaTime) triggered by the OnClick event. The function should only run ONCE
Any help regarding this is greatly appreciated! Thanks in advance

This is something you would want to use a coroutine for. something like this:

public IEnumerator myFunction() {
   int num = Random.Range(1, num1*num2);
    for(int i = 0; i < num; i++) {
        Vector3 currentX = objects*.transform.position.x;*

objects.transform.position = Vector3.MoveTowards(objects.transform.position, Vector3(currentX +1, objects_.transform.position.y, objects*.transform.position.z), Time.deltaTime);
yield return null;
}
}*_

void Update() {
if(Input.GetMouseDown(0)) {
StartCoroutine(myFunction);
}
}
Odds are this code wont compile EXACTLY as is (I just whipped it up), but this is the correct direction to go in. The coroutine will be executed each update, and continue where it left off. Once the loop is done, the coroutine will return and it will be done.
Read up on coroutines. they are very useful for stuff like this.