Need help setting up UI button functionality in coroutine

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). I’ve placed these methods 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.

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, perform all other functions (e.g., scoring, feedback, etc), 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
        }
    }
 }

Game playing is always = to true so the while loop inside the corutine will never stop as far as I know, I think that is the issue.

Thanks for your reply, though I’m not concerned about stopping the coroutine at the moment.

I’m just wondering if there is a way to present the puzzle, allow however long it takes for the player to press a button, then wait for 0.5 seconds before presenting a new puzzle. At the moment, the puzzle is presented, but does not wait for a player’s response before changing to the next puzzle. I’m not sure where I’m going wrong.

How about declaring a bool as a flag for your condition? The player clicks your button, you set the flag to true or false. Your other conditions check the bool and do something accordingly. If you have more than two states (true or false), you could use an enum. That would be my suggestion after skimming your code and reading @David_Lamonaca’s answer and yours.

Yes like I said I believe the issue is the while loop inside the coroutine, since gamePlaying is always = true the while loop will continue to run and the code inside it will continue to be ran so first you will get a new color then it will run the noresponse method followed by the yesresponse method then it will repeat the process.

That is my understanding of how the while loop works anyway.

Personally I dont think a coroutine or while loop is needed. Just assign the current puzzle at the start and then have the two methods assign to there appropriate button and upon being clicked it changes the puzzle.

I understand now - thank you very much for your help. I ended up not using a coroutine as you suggested.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.