Object reference not set to an instance of an object - Quiz game

Hello! 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.

You need to picture an array as an IKEA drawer
Before using it you have to put it together
This means, that you need to decide what the size of it will be
If you buy a drawer with 5 cases, then you can’t put anything inside the 6th
Of course even when you bought the drawer, you need to put it together, or it will only be a bunch of planks

int[] drawer; //Buying the drawer

void Start () {
    drawer = new int[5]; or drawer = {1,2,3,4,5}; //putting it together
    drawer[1] = 2; //putting something in the drawer
    drawer[5] = 2; //this results in an error because there is no 6th case
}

So, to add onto what @gorbit99 is saying. Your answer array is not initialized, so that is why you get the error.

Also to note, this

selection = Random.Range (0,2);

Will only return the values 0 and 1, since it’s an int, the 2 is exclusive. I would suggest you use

selection = Random.Range (0,questions.Length);

instead as that will adjust for the number of questions you have in case you add more or less.

Thank you all very much!
But a question @gorbit99 isn’t it what i’ve done? i initialized the array as you did i think… no? tell me what was the problem then, maybe i should’ve initialized the array on the start function and not on declaration?

@gorbit99 , @Brathnann

here is my code right now:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class QuestionGenerator : MonoBehaviour {

    public Question[] questions;



    public Text questionText;
    public Text[] answer;
    private int selection;

    void Start(){



        questions = new Question[3];
       
//        questions [0] = new Question ();
        questions [0].question = "Gannymede is the largest moon of which planet?";
        questions [0].correctAnswer = "Jupiter";
        questions [0].allAnswers [0] = "Mars";
        questions [0].allAnswers [1] = "Uranus";
        questions [0].allAnswers [2] = "Jupiter";
        questions [0].allAnswers [3] = "Neptune";

           

//        questions [1] = new Question();
        questions [1].question = "How many planets make up our Solar System?";
        questions [1].correctAnswer = "8";
        questions [1].allAnswers [0] = "7";
        questions [1].allAnswers [1] = "9";
        questions [1].allAnswers [2] = "6";
        questions [1].allAnswers [3] = "8";


//        questions [2] = new Question();
        questions [2].question = "One of the following is NOT an Overwatch hero";
        questions [2].correctAnswer = "Dokken";
        questions [2].allAnswers [0] = "Ana";
        questions [2].allAnswers [1] = "Tracer";
        questions [2].allAnswers [2] = "Dokken";
        questions [2].allAnswers [3] = "Pharah";



        selection = Random.Range (0,questions.Length);
        questionText.text  =  questions[selection].question;



    }


}

Still, Null Object Reference… Why is this happening??

why did you comment out the lines defining the array elements?

question[0] = new Question();

To continue using @gorbit99 's analogy, you have 3 holes in your yet-to-be-completed drawer, but just threw your content into the gaps without inserting the drawers themselves.

You did things right by adding a new question. However, your questions had an answer array (which is now allAnswers?) This was never initialized…This had to be initialized. That was your original error. You didn’t fix your error with the answers and now you have an error with the questions…