Wait till function was done by user click (at a chess game)

I’m programming a 2D chess game. The basic movement is working fine. Now I’m at the special moves. I want to start with the promotion.
I’m stuck here by some days.
What should happen.
When a player can move a pawn to the last row, he can choose a figure the pawn should become. For that, tha game should pause, a UI will apear at the side of the game and the player can click on the picture (it is a button) of the figure he wants that the pawn get. After he clicked the picutre, the pawn get’s changed and the game continues.
Now the problem is, that everything works exept the stop of the game, so the change can’t be done.
I have following codes.

I have the script Figure every figure has. In that script I have following code.

void OnMouseDown()
{
    GetComponentInParent<Board>().MoveFigureClickControl(this, null);
}

The Board script has following code

public void MoveFigureClickControl(Figure figure, Tile tile)
{
    if (figureInHand == false && tile == null)
    {
        activeFigure = figure;
        figureInHand = true;
    }
    else if (figureInHand == true && activeFigure != null && legalMove != null)
    {
        if (legalMove.Contains(tile.Get()))
        {
            if (tile.transform.childCount > 1)
            {
                Object.Destroy(tile.transform.GetChild(1).gameObject);
            }
            activeFigure.transform.SetParent(tile.transform, false);
            activeFigure.GetComponent<Figure>().SetNewPosition();

            if (this.GetComponent<MoveHandler>().Promotion(activeFigure))
            {
                if (activeFigure.figureColor)
                {
                    Debug.Log("if activeFigure.figureColor");
                    
                    inPromotion = true;
                    yield return WaitForPromotion();
                    Promotion_white.gameObject.SetActive(true);
                }
                else
                {
                    Debug.Log("if !activeFigure.figureColor");
                    
                    inPromotion = true;
                    yield return WaitForPromotion();
                    Promotion_black.gameObject.SetActive(true);
                }
            }

            figureInHand = false;
            activeFigure = null;
            ChangePlayer();
        }            
    }
}

private IEnumerable WaitForPromotion()
{
    Debug.Log("WaitForPromotion");
    while (inPromotion)
    {
        yield return new WaitForSeconds(3f);
    }
}

public void RookPromotion()
{
    (int, int) pawnPosition = activeFigure.GetCurrentPosition();
    string tileName = gameObject.GetComponent<MoveHandler>().GetTileName(pawnPosition.Item1, pawnPosition.Item2);
    Object.Destroy(gameObject.transform.Find(tileName).transform.GetChild(1).gameObject);
    GameObject newFigure;
    if (activeFigure.figureColor)
    {
        newFigure = Instantiate(rookPrefab_w);
        newFigure.transform.SetParent(gameObject.transform.Find(tileName), false);
        newFigure.GetComponent<Figure>().SetNewPosition();
    }
    else
    {
        newFigure = Instantiate(rookPrefab_b);
        newFigure.transform.SetParent(gameObject.transform.Find(tileName), false);
        newFigure.GetComponent<Figure>().SetNewPosition();
    }
    inPromotion = false;
    ResumeGame();
}

With the WaitForPromotion() I want the game to wait until with for example RookPromotion() was done. I call the RookPromotion() when I click on the rook picture of the UI.

But the problem is now, the WaitForPromotion() does not wait and it continous.

I hope it is clear what I want to do and somebody can help me.

Hello @Teand. What it looks like you need to do, is when your pawn gets to the end of the board, have your UI elements pop up, then pause the game using Time.timeScale.

This will allow you to stop the passage of time in the game. Time.timeScale does not stop the usage of buttons, so this should allow you to click your button, and when you run the function that allows you to change the pawn into another piece, set Time.timeScale back to 1.
Hope this helps! :slight_smile:

Docs: