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
}
}
}