Search Unity

Need help setting up UI button functionality in coroutine

Discussion in 'UGUI & TextMesh Pro' started by lalalanni, Jul 24, 2019.

  1. lalalanni

    lalalanni

    Joined:
    Jul 23, 2018
    Posts:
    5
    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!!

    Code (CSharp):
    1.  public class ColourGameManager : MonoBehaviour
    2. {
    3.      //reference
    4.      ColourLevelManager level;
    5.      //game variables
    6.      public bool gamePlaying;
    7.      //scoring variables
    8.      public int currentScore = 0;
    9.      public TextMeshProUGUI scoreText;
    10.      //feedback variables
    11.      public GameObject correctFeedback;
    12.      public GameObject incorrectFeedback;
    13.      public bool responseMade;
    14.      public Button YesButton;
    15.      public Button NoButton;
    16.  
    17.      private void Start()
    18.      {
    19.          StartCoroutine(StimulusRoutine());
    20.          gamePlaying = true;
    21.      }
    22.    
    23.      IEnumerator StimulusRoutine()
    24.      {
    25.          while(true)
    26.          {
    27.              while (gamePlaying == true)
    28.              {
    29.                  GetComponent<ColourLevelManager>().GetNextColour();
    30.                  EvaluateNoResponse();
    31.                  EvaluateYesResponse();
    32.                  yield return new WaitForSeconds(0.5f);
    33.              }
    34.              yield return new WaitForEndOfFrame();
    35.          }
    36.      }
    37.      //SCORING: award & add points to current score, then display on screen
    38.      public void AddToScore()
    39.      {
    40.          currentScore += level.pointsAwarded;
    41.          scoreText.text = currentScore.ToString();
    42.      }
    43.    
    44.      //RESPONSE EVALUATION
    45.      public void EvaluateNoResponse()
    46.      {
    47.          responseMade = true;
    48.          level = GetComponent<ColourLevelManager>();
    49.          if (level.randomTargetColourWord == level.randomStimulusColour)
    50.          {
    51.              level.correctResponse = false;
    52.          }
    53.          else if (level.randomTargetColourWord != level.randomStimulusColour)
    54.          {
    55.              level.correctResponse = true;
    56.              AddToScore();
    57.              //feedback
    58.          }
    59.      }
    60.      public void EvaluateYesResponse()
    61.      {
    62.          responseMade = true;
    63.          level = GetComponent<ColourLevelManager>();
    64.          if (level.randomTargetColourWord == level.randomStimulusColour)
    65.          {
    66.              level.correctResponse = true;
    67.              AddToScore();
    68.              //feedback
    69.          }
    70.          else if (level.randomTargetColourWord != level.randomStimulusColour)
    71.          {
    72.              level.correctResponse = false;
    73.              //feedback
    74.          }
    75.      }
    76.   }
     
  2. MSplitz-PsychoK

    MSplitz-PsychoK

    Joined:
    May 16, 2015
    Posts:
    1,278
    It looks like you use the "EvaluateResponse" methods for your buttons, but you are also calling them inside the coroutine. This will make it so your coroutine acts like it's pressing the buttons. Instead, you should tell your coroutine to wait until the buttons are pressed. Try changing your coroutine to look like this:


    Code (CSharp):
    1. IEnumerator StimulusRoutine()
    2. {
    3.     while(true)
    4.     {
    5.         while (gamePlaying == true)
    6.         {
    7.             // Set "responseMade" to false before each question
    8.             responseMade = false;
    9.             GetComponent<ColourLevelManager>().GetNextColour();
    10.  
    11.             // Wait until "responseMade" is set true by an "EvaluateResponse" method
    12.             yield return new WaitUntil(() => { return responseMade; });
    13.             yield return new WaitForSeconds(0.5f);
    14.         }
    15.         yield return new WaitForEndOfFrame();
    16.     }
    17. }