Problem with highlighting correct answer, any idea?
Hi guys, I am making an educational quiz game. I already have a lot of lines of code so I ll try to simplify my problem as much as I can. Lets say I have 4 UI texts for 4 answers. Each has a button component and a script component attached.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class Quiz : MonoBehaviour {
public string Answer;
public string correctAnswer;
void Start () {
GetComponent<Button>().onClick.AddListener(() => { Method(Answer); }); // this calls the method after I click on the text (where I have a button component)
}
public void Method (string passedAnswer) {
if (passedAnswer == correctAnswer)
{ GetComponent<Text> ().color = Color.green; // if I click on correct answer, the text color changes to green
}
else
{GetComponent<Text>().color = Color.red;
// HERE, how do I find a UI text where the answer variable equals correct answer? (so that I could highlight it to green?)
}
}
}
Although it is super easy to highlight the color of text to correct/incorrect color, my problem is this > when I click on incorrect answer how do I find a text where the answer variable equals correctAnswer? (So that I could highlight it to green color or whatever). Thanks a lot in advance!
Comment
Best Answer
Answer by ElDo · Oct 28, 2015 at 07:58 PM
You could try it like this:
Quiz[] q=GameObject.FindObjectsOfType<Quiz>();
Quiz correct=null;
foreach(Quiz c in q){
if(c.Answer==correctAnswer)correct=c;
}
but this code is not tested. The idea behind is to get all Objects and compare to the one that has the correct answer.