Question by
jackhamlingthon · May 06 at 09:51 AM ·
nullreferenceexceptionjsondataquiz
Select correct answer in a quiz game
Currently working on a project that requires making a quiz game in unity with data coming from a firebase web api
I was able to retreive data an put them in text fields and buttons in the scene but i couldn't figure out a way to make the logic of the game, how to identify the correct answer and add score.
This is my answer code
public void Answer()
{
if (isCorrect)
{
GetComponent<Image>().color = Color.green;
Debug.Log("Correct Answer");
theQuiz.correct();
}
else
{
GetComponent<Image>().color = Color.red;
Debug.Log("Wrong Answer");
theQuiz.wrong();
}
}
And this is my correct answer verification code
var quest = JsonConvert.DeserializeObject<QuestionResponse>(response.downloadHandler.text);
if (quest.documents.Count >= 0)
{
currentQuestion = Random.Range(0, quest.documents.Count);
QuestionTxt.text = quest.documents[currentQuestion].fields.q.stringValue;
Ch1Txt.text = quest.documents[currentQuestion].fields.ch1.stringValue;
Ch2Txt.text = quest.documents[currentQuestion].fields.ch2.stringValue;
Ch3Txt.text = quest.documents[currentQuestion].fields.ch3.stringValue;
Ch4Txt.text = quest.documents[currentQuestion].fields.ch4.stringValue;
Text c1 = Ch1Txt.GetComponent<Text>();
totalQuestions = quest.documents.Count;
c1.GetComponent<AnswerScriptApi>().isCorrect = true;
When i execute this a NullReferenceException: Object reference not set to an instance of an object appears
Comment