I want to highlight the correct answer when wrong one clicked... Help!
Hey everyone,
I am working on Trivia based game, and I am new at Unity and C#. I just want to highlight correct answer when wrong answer clicked... My code is like;
public void checkAnswer(int buttonNum)
{
if (allowSelection)
{
if (buttonNum == currentQuestion.correctAnswerIndex)
{
print("TRUE");
numberOfCorrectAnswers++;
feedbackText.GetComponent<Text>().text = "TRUE";
feedbackText.GetComponent<Text>().color = Color.green;
AudioSource audio = GetComponent<AudioSource>();
audio.Play();
audio.clip = CorrectAnswerSound;
audio.Play();
playerScore += pointsAddedForCorrectAnswer;
scoreDisplayText.text ="Score: "+ playerScore.ToString();
}
else
{
print("FALSE");
feedbackText.GetComponent<Text>().text = "FALSE";
feedbackText.GetComponent<Text>().color = Color.red;
AudioSource audio = GetComponent<AudioSource>();
audio.Play();
audio.clip = WrongAnswerSound;
audio.Play();
GetComponent<Button>().image.color = Color.green;
I've tried "GetComponent().image.color = Color.green;" but ı get " object reference not set instance of object " so any ideas, how ı can set that reference..
Or any other method for highlighting the answers?
Best Regards!
Answer by tormentoarmagedoom · Mar 10, 2020 at 12:03 PM
Hello! This is a standard answer.
For Null Reference problems, you need to learn to find your problem by your own. As you can imagine, we can not try to read all scripts people posts, understand the logic and process, what all variables means, when or how you use them, and find where is the "problem". You are the only one who can do it...
Forst, check your error code, it says the line where the problem is. You need to debug the code while running, and check the states of the variables of that line at the moment you see the problem, and I'm sure you will detect what variable is still NULL. Then investigate why.
Look for some tutorials on how to debug code while running on your scripting software if don't know what I'm talking about.
Bye & good Luck!