How to call array element that has bool value true unity C#

How to call array element that has bool value true unity C#



I have four buttons using the same prefab and holding 4 text elements from an array out of which only one is assigned bool value to true. I am trying to access that true element when any of false element is clicked. i want to highlight true element when the false element is clicked. can anyone please help me to achieve this functionality?
using simpleobjectpool
taking reference from unity quiz game tutorial
Thanks



Answer Button Script


public class AnswerButton : MonoBehaviour
{
public Text answerText;
private AnswerData answerData;
private GameController gameController;
private bool isCorrect;

void Start()

gameController = FindObjectOfType<GameController>();


public void Setup(AnswerData data)

answerData = data;
answerText.text = answerData.answerText;



public void HandleClick()
{
gameController.AnswerButtonClicked(answerData.isCorrect);

if (answerData.isCorrect)




if (!answerData.isCorrect)








Answer Data Script


using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[System.Serializable]
public class AnswerData

public string answerText;
public bool isCorrect;



QuestionData Script


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class QuestionData


public string questionText;
public AnswerData answers;



Game Controller Script


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using System;

public class GameController : MonoBehaviour

public Text questionText;
public Text scoreDisplayText;
public SimpleObjectPool answerButtonObjectPool;
public Transform answerButtonParent;
public GameObject questionPanel;
public GameObject roundOverPanel;
public GameObject levelsPanel;
private DataController dataController;
private RoundData currentRoundData;

private bool isRoundActive;
private float timeBeetweenQuestions = 3.0f;

private List<GameObject> answerButtonGameObjects = new List<GameObject>();

private QuestionData questionPool;
private int questionIndex;
private int qNumber = 0;
private List<int> questionIndexesChosen = new List<int>();
public int playerScore = 0;
public int totalQuestions;


private static int pointAddedForCorrectAnswer;

public AudioSource answerButtonClicked;
public AudioSource wrongAnswerClicked;


void Start ()

dataController = FindObjectOfType<DataController>();
currentRoundData = dataController.GetCurrentRoundData();
questionPool = currentRoundData.questions;

playerScore = 0;
questionIndex = 0;
scoreDisplayText.text = "Score: " + playerScore.ToString();
isRoundActive = true;
ShowQuestion();




private void ShowQuestion()

RemoveAnswerButtons();

QuestionData questionData = questionPool[questionIndex];
questionText.text = questionData.questionText;

for (int i = 0; i < questionData.answers.Length; i++)



GameObject answerButtonGameObject =
answerButtonObjectPool.GetObject();

answerButtonGameObjects.Add(answerButtonGameObject);

answerButtonGameObject.transform.SetParent(answerButtonParent);

AnswerButton answerButton =
answerButtonGameObject.GetComponent<AnswerButton>();
AnswerButton.Setup(questionData.answers[i]);




private void RemoveAnswerButtons()

while (answerButtonGameObjects.Count > 0)


answerButtonObjectPool.ReturnObject(answerButtonGameObjects[0]);
answerButtonGameObjects.RemoveAt(0);



IEnumerator TransitionToNextQuestion()


yield return new WaitForSeconds(timeBeetweenQuestions);
ShowQuestion();


IEnumerator WaitForFewSeconds()


yield return new WaitForSeconds(timeBeetweenQuestions);
EndRound();

IEnumerator ReturnCorrectButtonColor()

Debug.Log("im correct");
GetComponent<Button>().image.color = Color.green;
yield return new WaitForSeconds(seconds: 2.9f);
GetComponent<Button>().image.color = Color.white;



IEnumerator ReturnWrongButtonColor()

Debug.Log("im wrong");
GetComponent<Button>().image.color = Color.red;
yield return new WaitForSeconds(seconds: 2.9f);
GetComponent<Button>().image.color = Color.white;



public void AnswerButtonClicked (bool isCorrect)


if (isCorrect)

playerScore += currentRoundData.pointAddedForCorrectAnswer;
scoreDisplayText.text = "Score: " + playerScore.ToString();

//play coorect answer sound
answerButtonClicked.Play();
StartCoroutine(ReturnCorrectButtonColor());


if (!isCorrect)

//play wrong answer sound
answerButtonClicked = wrongAnswerClicked;
answerButtonClicked.Play();
StartCoroutine(ReturnWrongButtonColor());
// buttons = GameObject.FindGameObjectsWithTag("Answer");

//
// foreach (GameObject button in buttons)
//
// if (button.GetComponent<AnswerButton>
//().answerData.isCorrect)
//
// button.GetComponent<AnswerButton>
// ().StartCoroutine(ReturnCorrectButtonColor());
//
//
//



if (qNumber < questionPool.Length - 1) /

qNumber++;
StartCoroutine(TransitionToNextQuestion());


else


StartCoroutine(WaitForFewSeconds());



public void EndRound()

isRoundActive = false;
questionPanel.SetActive(false);
roundOverPanel.SetActive(true);



//on button click return to main menu
public void ReturnToMenu ()

SceneManager.LoadScene("MenuScreen");









What is wrong with the code you currently have? Also there's a lot missing, please provide a Minimal, Complete, and Verifiable example
– UnholySheep
Aug 22 at 6:59





Where is your collections of buttons?
– aloisdg
Aug 22 at 7:00





@UnholySheep sorry for not entering all scripts .here i have edited my question .thanks
– Sarita
Aug 22 at 7:55





AnswerData answers holding four text elements with bool value attached out of which only one is true. and these are inside answer button which is a prefab. i am able to change button colors when it is true or false however i want to highlight true element in case false element is clicked
– Sarita
Aug 22 at 7:59





@Sarita the GameController script seems corrupted, there is no AnswerButtonClicked method, but some random code lines (those starting with "if (qNumber < questionPool.Length - 1)" ) at the end.
– rs232
Aug 22 at 8:01




1 Answer
1



In order to highlight both Wrong (the clicked one) and the Right buttons you need to have access to both buttons. This means that you can't do highlighting from HandleClick method of your Answer Button Script, as it only has access to itself, e.g. to the Wrong button.



The good thing is that this method notifies GameController that the button has been clicked. GameController knows about all the buttons, so it can easily highlight both buttons.



So, instead of launching your highlight subroutines from Answer Button's HandleClick, you should do this from GameController's AnswerButtonClicked: identify both the Right button and the clicked button there and launch appropriate subroutines for them.



Update:



For instance, your ReturnCorrectButtonColor would look like:


IEnumerator ReturnCorrectButtonColor( GameObject button )

Debug.Log("im correct");
button.GetComponent<Button>().image.color = Color.green;
yield return new WaitForSeconds(seconds: 2.9f);
button.GetComponent<Button>().image.color = Color.white;



so in AnswerButtonClicked you identify which button to highlight as a correct answer button and pass it as a paramter to this method:


StartCoroutine(ReturnCorrectButtonColor(correctButton));





Thanks. surely I will move it to game controller script. but I am still not able to understand how to access both buttons at the same time. as they all have the same script attached. I tried using for foreach but then it doesn't work.
– Sarita
Aug 22 at 8:33





But why would you need to access them at the same time? You just find the Right answer button and start a subroutine for it, then you find the clicked Wrong answer button ans start another subroutine for it. That's all. The subroutines will change their button's colors automatically, you don't need to worry about that.
– rs232
Aug 22 at 8:35





ok. i will try that and update what happens. thank you very much for your time and guidance.
– Sarita
Aug 22 at 8:40





I tried your suggestion but when I click the wrong answer it is changing it to green. instead of highlighting the correct answer to green. How can I access button that is holding correct answer when the wrong answer is clicked? still not able to understand.
– Sarita
Aug 23 at 11:22






Can you please share the code you've tried? As for accessing the correct answer button: your game controller has all the information about all the buttons, right? It knows which (wrong) button was pressed as it gets this information as an input parameter for AnswerButtonClicked method. It can easily find which answer is correct as it knows all the answers. Then it should just display the wrong button as red and the correct button as green and it has everything it needs to do that.
– rs232
Aug 23 at 11:30







By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)