问题
I'm doing a multiple choice quiz game, and im tryinng to highlight the correct answer in green if the wrong answer is clicked. for example if i have A , B , C and D and "A" is the correct answer. i want if i choose "B" to turn B to red color and display the correct answer to green which is in this case "A". Right now i have if i click the right answer it displays green, as i want it. and if i press wrong answer it displays red. what im missing is when i click wrong answer i want to also highlight the correct answer.
Here's my functions:
In the Game controller:
public bool theAnswerIsCorrect;
public bool IsCorrected()
{
return theAnswerIsCorrect;
}
public void AnswerButtonClick(bool isCorrect)
{
if (isCorrect)
{
Debug.Log("I'm Correct");
theAnswerIsCorrect = true;
playerScore += currentRoundData.pointAddedForCorrectAnswer;
scoreDisplayText.text = "Score: " + playerScore.ToString();
}
else
theAnswerIsCorrect = false;
// Do we still have questions?
if (questionPool.Length > questionIndex + 1)
{
//questionIndex++;
UpdateQuestionIndex();
StartCoroutine(DelayTime(3));
// ShowQuestion();
}
else
{
EndRound();
}
}
And this is an a class that just holdes my data
public class answerData {
public string answerTxt;
public bool isCorrect;
}
This is being used in the ButtonClick function (the code in the begging of the question) -> this line
gameController.AnswerButtonClick (AnswerData.isCorrect);
And in the inspector i specify by the bool what is the correct answer
*** NEW SCRIPT HERE "Picking up the Correct button"
// store reference to btn text
public Text answerText;
private answerData AnswerData;
private GameController gameController;
public static AnswerButton _instace;
private void Awake()
{
_instace = this;
}
// Use this for initialization
void Start ()
{
gameController = FindObjectOfType<GameController> ();
}
public void Setup(answerData data)
{
AnswerData = data;
answerText.text = AnswerData.answerTxt;
}
IEnumerator ReturnButtonColor()
{
yield return new WaitForSeconds(2.9f);
GetComponent<Button>().image.color = Color.white;
Debug.Log("HiB");
}
public void HandleClick()
{
gameController.AnswerButtonClick (AnswerData.isCorrect);
if (gameController.IsCorrected())
{
GetComponent<Button>().image.color = Color.green;
Debug.Log("im true");
StartCoroutine(ReturnButtonColor());
}
else
{
GetComponent<Button>().image.color = Color.red;
// gameController.IsCorrected().image.color = Color.green;
StartCoroutine(ReturnButtonColor());
}
}
Thank you
回答1:
I am assuming that all option button have same script attached.
Create one delegate and register this in your script which is attached to your option button.
Like this
Create delegate and event
#region DELEGATES
public delegate void OnQuestionOptionClicked ();
public static event OnQuestionOptionClicked onQuestionOptionClicked;
#endregion
#region DELEGATE_CALLS
private void RaiseOnQuestionOptionClicked ()
{
if (onQuestionOptionClicked != null)
onQuestionOptionClicked ();
}
#endregion
Register it
void OnEnable ()
{
onQuestionOptionClicked += OnQuestionOptionClicked;
}
void OnDisable ()
{
onQuestionOptionClicked -= OnQuestionOptionClicked;
}
#region DELEGATE_EVENT_LISTENER
void OnQuestionOptionClicked ()
{
GetComponent<Button>().interactable = false;
if (AnswerData.isCorrect){
GetComponent<Button>().image.color = Color.green;
Debug.Log("im true");
}
}
#endregion
Your setup method
public void Setup(answerData data)
{
AnswerData = data;
answerText.text = AnswerData.answerTxt;
GetComponent<Button>().interactable = true;
GetComponent<Button>().image.color = Color.white;
}
And on button click event
#region BUTTON_CLICK_LISTENER
public void ButtonClick()
{
gameController.AnswerButtonClick (AnswerData.isCorrect);
if (!gameController.IsCorrected())
{
GetComponent<Button>().image.color = Color.red;
Debug.Log("im true");
// StartCoroutine(ReturnButtonColor());
}
RaiseOnQuestionOptionClicked ();
}
#endregion
Hope this solution helps you.;)
回答2:
Like using GetComponent<Button>().image.color = Color.green;
in the if
statement , you could simply add in /*Button A*/.image.color = Color.green;
in the else
statement.
回答3:
What @BadWolf in the comments is saying is the correct answer. If you need more coding help you have to include more information. How do you determine the Correct Button
? Does it have a special name in the Scene? Does the gameController
know which button is the correct? It seems gameController knows which Button is the correct one so you should create a method which will look something like:
public Button GetCorrectButton(){
return theCorrectButton;
}
I don't know the code for how you see if it is the correct button but I'm guessing you have some way where you use the Correct Button. Find the Correct Button and return it in the method GetCorrectButton.
which you will then use in your code like:
public void ButtonClick()
{
gameController.AnswerButtonClick (AnswerData.isCorrect);
if (gameController.IsCorrected())
{
GetComponent<Button>().image.color = Color.green;
Debug.Log("im true");
// StartCoroutine(ReturnButtonColor());
}
else
{
GetComponent<Button>().image.color = Color.red;
// Like this:
gameController.GetCorrectButton().image.color = Color.green;
}
}
I can be more specific if I get some more information/code to see!
来源:https://stackoverflow.com/questions/41690670/unity-quiz-game-multiple-choice-button-colors