Wait for button response in coroutine?
Asked Answered
C

2

0

I’ve got a colour puzzle-type game and have 2 UI buttons that I’m trying to set up functionality for - a “YES” button and a “NO” button. If the colours match, you need to press the YES button and if they don’t you press the NO button.

Currently, I’m using the On Click function in the inspector for each button. I’ve got a method for the yes button (EvaluateYesResponse) and a separate method for the no button (EvaluateNoResponse). So, for the NO button I’ve selected the EvaluateNoResponse function, and similarly for the YES button.
These methods are currently in a coroutine so that: a new colour is presented, player makes a response by pressing yes or no, a score is awarded, then after 0.5 seconds, the next colour is presented.

At the moment, my button functionality isn’t working even though I’ve made a reference to the buttons and assigned them in the inspector. When I press play, the colours just keep switching without waiting for the player’s response.

I suspect that I’m going wrong by placing the two evaluate methods in the coroutine but I’m not sure where else to call them. Essentially what I am hoping for is to present the colour puzzle, then wait for the player’s response by pressing the yes or no button, then present the next puzzle. Any help with setting up these buttons would be greatly appreciated!!

public class ColourGameManager : MonoBehaviour
{
    //reference
    ColourLevelManager level;
    
    //game variables
    public bool gamePlaying;
    
    //scoring variables
    public int currentScore = 0;
    public TextMeshProUGUI scoreText;
    
    //feedback variables
    public GameObject correctFeedback;
    public GameObject incorrectFeedback;
    public bool responseMade;
    public Button YesButton;
    public Button NoButton;
   
    private void Start()
    {
        StartCoroutine(StimulusRoutine());
        gamePlaying = true;
    }
    
    IEnumerator StimulusRoutine()
    {
        while(true)
        {
            while (gamePlaying == true)
            {
                GetComponent<ColourLevelManager>().GetNextColour();
                EvaluateNoResponse();
                EvaluateYesResponse();
                yield return new WaitForSeconds(0.5f);
            }
            yield return new WaitForEndOfFrame();
        }
    
    }
    
    //SCORING: award & add points to current score, then display on screen
    public void AddToScore()
    {
        currentScore += level.pointsAwarded;
        scoreText.text = currentScore.ToString();
    }
    
    //RESPONSE EVALUATION
    public void EvaluateNoResponse()
    {
        responseMade = true;
        level = GetComponent<ColourLevelManager>();
        if (level.randomTargetColourWord == level.randomStimulusColour)
        {
            level.correctResponse = false;
        }
        else if (level.randomTargetColourWord != level.randomStimulusColour)
        {
            level.correctResponse = true;
            AddToScore();
            //feedback
        }
    }
    
    public void EvaluateYesResponse()
    {
        responseMade = true;
        level = GetComponent<ColourLevelManager>();
        if (level.randomTargetColourWord == level.randomStimulusColour)
        {
            level.correctResponse = true;
            AddToScore();
            //feedback
        }
        else if (level.randomTargetColourWord != level.randomStimulusColour)
        {
            level.correctResponse = false;
            //feedback
        }
    }
}
Contaminate answered 17/4, 2024 at 16:56 Comment(0)
S
0

Since this is a quite common issue / task to wait for an UI button press in a coroutine, I just created a custom yield instruction that will handle this for you. I added it to the WaitForUIButtons wiki:

UI/WaitForUIButtons - Unify Community Wiki (archive.org)

Here’s also a pastebin backup of the script:
WaitForUIButtons.cs - Pastebin.com

You can provide it a list of UI buttons and this class will register temporary callbacks to all those buttons. When one of the buttons is pressed the internal PressedButton property will be set to the button that was pressed. So you can simply do:

// from my wiki examples
public Button yesButton;
public Button noButton;

IEnumerator Dialog()
{
    // ...
    var waitForButton = new WaitForUIButtons(yesButton, noButton);
    yield return waitForButton.Reset();
    if (waitForButton.PressedButton == yesButton)
    {
        // yes was pressed
    }
    else
    {
        // no was pressed
    }
    // ...
}
Squarerigger answered 17/4, 2024 at 17:2 Comment(2)

Such a simple solution, thanks man!

Posada

Thank you!!

Contaminate
P
0

In your code, the coroutine is in while loop with a gamePlaying true, and as gamePlaying is always true that’s why it is not waiting for the input. You can declare another boolean as in _isWaitOver and make it true on button click

while (gamePlaying == true)
{
    if(_isWaitOver)
        GetComponent().GetNextColour();
    EvaluateNoResponse();
    EvaluateYesResponse();
    yield return new WaitForSeconds(0.5f);
}
Poem answered 17/4, 2024 at 16:57 Comment(0)
S
0

Since this is a quite common issue / task to wait for an UI button press in a coroutine, I just created a custom yield instruction that will handle this for you. I added it to the WaitForUIButtons wiki:

UI/WaitForUIButtons - Unify Community Wiki (archive.org)

Here’s also a pastebin backup of the script:
WaitForUIButtons.cs - Pastebin.com

You can provide it a list of UI buttons and this class will register temporary callbacks to all those buttons. When one of the buttons is pressed the internal PressedButton property will be set to the button that was pressed. So you can simply do:

// from my wiki examples
public Button yesButton;
public Button noButton;

IEnumerator Dialog()
{
    // ...
    var waitForButton = new WaitForUIButtons(yesButton, noButton);
    yield return waitForButton.Reset();
    if (waitForButton.PressedButton == yesButton)
    {
        // yes was pressed
    }
    else
    {
        // no was pressed
    }
    // ...
}
Squarerigger answered 17/4, 2024 at 17:2 Comment(2)

Such a simple solution, thanks man!

Posada

Thank you!!

Contaminate

© 2022 - 2025 — McMap. All rights reserved.