Question by
siaknimarkanthony · Feb 08, 2018 at 02:25 PM ·
arraytextarraylist
How to get text from array or in bool?
Hi everyone. i am try to make a quiz game and i want to show the correct answer if the player hits the wrong answer. can you someone help me?
I have a script that holds the correct answers
public class AnswerData
{
public string answerText;
public bool isCorrect;
}
and array by this script
public class QuestionData
{
public string questionText;
public AnswerData[] answers;
public AudioClip[] questionAudio;
}
and called in my game controller script if the answer is correct
public void AnswerButtonClicked(bool isCorrect)
{
if (isCorrect)
{
playerScoreOne += currentRoundData.pointsAddedForCorrectAnswer;
scoreDisplayText.text = "Score: " + playerScoreOne.ToString();
rightAnswerPanel.SetActive(true);
questionDisplay.SetActive(true);
StartCoroutine(TimeBetweenQuestions());
}
else
{
wrongAnswerPanel.SetActive(true);
StartCoroutine(TimeBetweenQuestions());
}
Comment
Best Answer
Answer by Hellium · Feb 08, 2018 at 02:56 PM
Simply use Array.Find.aspx) to retrieve the correct answer, or simply make sure the correct answer is always the first one in your answers
array.
if (isCorrect)
{
// ...
}
else
{
AnswerData correctAnswer = System.Array.Find( answers, answer => answer.isCorrect ) ;
if( correctAnswer != null )
{
// Show text using correctAnswer.answerText
}
wrongAnswerPanel.SetActive(true);
StartCoroutine(TimeBetweenQuestions());
}
I got an error in the AnswerButtonClicked says the type on name The type or namescpace could not be found.
Bad copy-paste, sorry. Replace AnswerButtonClicked
by AnswerData