This question already has an answer here:
I am trying to make a Unity quiz game when u get random questions but it's not working it's showing me "Object reference not set to an instance of an object" and pointing to line 24, where i declare the array of questions.
QuestionGenerator class:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class QuestionGenerator : ScriptableObject {
private Question[] questions = new Question[3];
public Text question;
public Text[] answer;
private int selection;
void Start(){
questions [0] = new Question ();
questions [0].question = "Gannymede is the largest moon of which planet?";
questions [0].correctAnswer = "Jupiter";
questions [0].answers [0] = "Mars";
questions [0].answers [1] = "Uranus";
questions [0].answers [2] = "Jupiter";
questions [0].answers [3] = "Neptune";
questions [1] = new Question ();
questions [1].question = "How many planets make up our Solar System?";
questions [1].correctAnswer = "8";
questions [1].answers [0] = "7";
questions [1].answers [1] = "9";
questions [1].answers [2] = "6";
questions [1].answers [3] = "8";
questions [2] = new Question ();
questions [2].question = "One of the following is NOT an Overwatch hero";
questions [2].correctAnswer = "Dokken";
questions [2].answers [0] = "Ana";
questions [2].answers [1] = "Tracer";
questions [2].answers [2] = "Dokken";
questions [2].answers [3] = "Pharah";
selection = Random.Range (0,2);
question.text = questions[selection].question;
}
// Update is called once per frame
void Update () {
}
}
My question class (The base question class Question.cs):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Question : MonoBehaviour {
public string correctAnswer;
public string[] answers;
public string question;
public string subject;
}
I've tried everything, retyping the whole deal and also cleaning, and nothing is working. please help! thank you very much.
Aucun commentaire:
Enregistrer un commentaire