(SOLVED (w/Workaround)) Help with coroutines, I need a method to run after another has ended

I have a classic “character movement” code, in which characters moves in turns controlled by independent target positions, like this:

void Update () {
    if (currentMover == null) {

        currentMover = this;

    }

    if (currentMover == this) {       

        chooseEnemyDir (x, z, targetX, targetZ);    // method that calculate the enemy direction...
                // ...and sets "targetPosition" and turns "isMoving" to true;   

    }           

    if isMoving {

        transform.position = Vector3.MoveTowards (transform.position, targetPosition, speed * Time.deltaTime);

        if (transform.position == targetPosition) {
            isMoving = false; doneMoving = true; currentMover = null;
        }

    }

} // end update

int chooseEnemyDir (x, z, targetX, targetZ) {

    // this calculates where to move the enemy based on the player coordinates

}

Until here everything works well, but I need that at some moment (decided by a counter) the enemy moves a sector of the ground: MoveSector(). The method gets called at the beginning of the enemy turn, so when the character moves the sector, all entities in there get translated to their new positions:

void Update () {
    if (currentMover == null) {

        currentMover = this;

    }

    if (currentMover == this) {       

        if (canMoveSector == 10 & !isMoving & doneMoving) {
            // boolean canMoveSector is defined by a "counter++"
            // in this case, every 10 turns in the same sector

            MoveSector(sectorToMove); // this performs the sector movement
        }

        chooseEnemyDir (x, z, targetX, targetZ);    // method that calculate the enemy direction...
                // ...and sets "targetPosition" and turns "isMoving" to true;   

    }           

    if isMoving {

        transform.position = Vector3.MoveTowards (transform.position, targetPosition, speed * Time.deltaTime);

        if (transform.position == targetPosition) {
            isMoving = false; doneMoving = true; currentMover = null;
        }

    }

} // end update

int chooseEnemyDir (x, z, targetX, targetZ) {

    // this calculates where to move the enemy based on the player coordinates

}

void moveSector (sectorToMove) {

    //moves the sector along with the entities there, and returns to move the character in turn

}

The MoveSector method recalculates all positions of the characters standing in that sector, and translate them accordingly. But I noticed that when two characters are in the same sector and one of them moves the sector, one of them moves in a wrong direction and ends up in a wrong position. That’s because (I guess) the MoveSector() method gets executed when the movement of the character hasn’t reached it’s target position. MoveSector() does it fine when none of the characters is moving. I’ve tried a lot of options but none of them works.

So what I need is to know the correct way to put the call to MoveSector “in wait” to be performed when all other characters have moved. I know that maybeit has to be done with a Coroutine and iEnumerator, but I’m not sure how to implement it. Please help.

You could use delegates to fire events when they have reached their destinations ? Or flags so you can check if the other characters have stopped?

I’ve used flags, in fact “isMoving” is a flag… I’ll check delegates (I’m not expert in Unity yet), but what I need is: character reaches target position → move the sector IF everyone is still (or) wait to move only until someone finish their movement → next character move.

I think what I’m really looking for is: put this “MoveSector()” on queue until the current moving enemy finishes its movement… and wait to move other enemy until MoveSector finishes. That’s why I thought that I had to use coroutines, but I still don’t understand it very well. Any advice?

I just solved this but with a workaround: I made that two enemies were unable to move the same sector at the same time. That avoids the conflict of position. Not so elegant but it works. If some of you thinks about another solution, of course it will be nice.