How do I treat a custom class as an Array? (Indexing)

Hi guys, I’m new to this board so hello :slight_smile:

I’m make a simple quiz game. I’m using C#. I’d like to make a ‘question’ class that includes a few properties like ints and strings.

using UnityEngine;
using System.Collections;

public class Question {
	public string question;
	public string[] answer = new string[3];
	public int correctAnswer;
}

But when I try to make an array of ‘questions’ like this -

Question[] myQuestion;
for(int i=0; i<5; i++){
	myQuestion *= new Question();*

}
I get this nasty error…
"NullReferenceException: Object reference not set to an instance of an object
(wrapper stelemref) object:stelemref (object,intptr,object)"
Any ideas on how to create an array of custom class objects?

you need to make the array before the loop

Question[] myQuestion = new Question[5]; //edited....must stop answering questions early in the morning.

as you have done with the answer variable.

you could also look into lists, as you do not have to declare the size of them on initialisation.