I have a Serializable Class, I want to create its array in other script
[Serializable]
public class Question
{
public string fact;
}
Now here I have another class that creates and initializes array of Question.
public class Initialize : MonoBehaviour
{
Question[] questions;
void Start()
{
questions = new Question[1];
questions[0].fact = "First Question"; // Here I am getting NullReferenceException
}
}
Another interesting thing to mention is when I use IEnumerator to initialize same array and yield return null at beginning the code works as expected, Here is the code
public class Initialize : MonoBehaviour
{
Question[] questions;
void Start()
{
questions = new Question[1];
StartCoroutine(InitializeArray());
}
IEnumerator InitializeArray()
{
yield return null;
questions[0].fact = "First Question";
}
}
When I use yield statement before initializing it works fine, when I comment out yield statement again NullReferenceException strikes, How this is even possible?